2012-01-21 9 views
0

DataSourceのItemsSourceがBindingListであるWPFアプリケーションがあります。 BindingList内のオブジェクトを変更すると、DataGridが更新されていません。 BindingListのオブジェクトを変更するときに、データグリッドが更新されるようにするにはどうすればよいですか?ここでバインディングリストのオブジェクトプロパティが変更されたときにDataGridセルが更新されない

はXAMLです:ここでは

<DataGrid CanUserAddRows="False" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Name="dgLocalPlugins" VerticalAlignment="Stretch" SelectionMode="Single" AlternatingRowBackground="#CDEBEBEB" Margin="5,85,5,143" Width="Auto" SelectionChanged="dgLocalPlugins_SelectionChanged"> 
    <DataGrid.Columns> 
     <DataGridCheckBoxColumn Header="Enabled" Binding="{Binding Path=Enabled}" Width="55" > 
      <DataGridCheckBoxColumn.CellStyle> 
       <Style> 
        <EventSetter Event="CheckBox.Checked" Handler="OnChecked"/> 
        <EventSetter Event="CheckBox.Unchecked" Handler="OnUnchecked"/> 
       </Style> 
      </DataGridCheckBoxColumn.CellStyle> 
     </DataGridCheckBoxColumn> 
     <DataGridTextColumn Header="Plugin" Binding="{Binding Path=Type}" IsReadOnly="True" Width="*" /> 
     <DataGridTextColumn Header="Status" Binding="{Binding Path=Status}" IsReadOnly="True" Width="*" /> 
    </DataGrid.Columns> 
</DataGrid> 

は私がするBindingListを移入どこの背後に関連するコードです:ここでは

private BindingList<PluginDescription> pluginList = new BindingList<PluginDescription>(); 

foreach (string path in osapdFiles) 
{ 
    if (!string.IsNullOrEmpty(path)) 
    { 
     PluginDescription desc = PluginHelper.Deserialize(path); 
     pluginList.Add(desc); 
    } 
} 

dgLocalPlugins.ItemsSource = pluginList; 

はPluginDescriptionクラスです:今

public class PluginDescription : INotifyPropertyChanged 
{ 
    private string _pluginName; 
    public string Name 
    { 
     set 
     { 
      if (value != this._pluginName) 
      { 
       this._pluginName = value; 
       NotifyPropertyChanged("Name"); 
      } 
     } 
     get { return _pluginName; } 
    } 

    private string _pluginType; 
    public string Type 
    { 
     set 
     { 
      if (value != this._pluginType) 
      { 
       this._pluginType = value; 
       NotifyPropertyChanged("Type"); 
      } 
     } 
     get { return _pluginType; } 
    } 

    private string _pluginVersion; 
    public string Version 
    { 
     set 
     { 
      if (value != this._pluginVersion) 
      { 
       this._pluginVersion = value; 
       NotifyPropertyChanged("Version"); 
      } 
     } 
     get { return _pluginVersion; } 
    } 

    private string _pluginAuthor; 
    public string Author 
    { 
     set 
     { 
      if (value != this._pluginAuthor) 
      { 
       this._pluginAuthor = value; 
       NotifyPropertyChanged("Author"); 
      } 
     } 
     get { return _pluginAuthor; } 
    } 

    private string _wikiUrl; 
    public string WikiUrl 
    { 
     set 
     { 
      if (value != this._wikiUrl) 
      { 
       this._wikiUrl = value; 
       NotifyPropertyChanged("Wiki"); 
      } 
     } 
     get { return _wikiUrl; } 
    } 

    private string _description; 
    public string Description 
    { 
     set 
     { 
      if (value != this._description) 
      { 
       this._description = value; 
       NotifyPropertyChanged("Description"); 
      } 
     } 
     get { return _description; } 
    } 

    private string _status; 
    public string Status 
    { 
     set 
     { 
      if (value != this._status) 
      { 
       this._status = value; 
       NotifyPropertyChanged("Statua"); 
      } 
     } 
     get { return _status; } 
    } 

    private bool _enabled; 
    public bool Enabled 
    { 
     set 
     { 
      if (value != this._enabled) 
      { 
       this._enabled = value; 
       NotifyPropertyChanged("Enabled"); 
      } 
     } 
     get { return _enabled; } 
    } 

    private string _upgrade; 
    public string Upgrade 
    { 
     set 
     { 
      if (value != this._upgrade) 
      { 
       this._upgrade = value; 
       NotifyPropertyChanged("Upgrade"); 
      } 
     } 
     get { return _upgrade; } 
    } 

    private string _path; 
    public string Path 
    { 
     set 
     { 
      if (value != this._path) 
      { 
       this._path = value; 
       NotifyPropertyChanged("Path"); 
      } 
     } 
     get { return _path; } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(String info) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(info)); 
     } 
    } 

} 

、 PluginDescriptionオブジェクトの1つのプロパティを変更すると、DataGridが更新されていません。私は、PluginDescriptionクラスのINotifyPropertyChangeインターフェイスを実装することがすべて私が必要と思った。私は何か別のことをする必要がありますか?

答えて

1

PropertyChangedイベントには、プロパティの正確な名前を指定する必要があります。あなたのコードでは、いくつかのミスマッチがあります。プロパティは、"Wiki"PropertyChangedイベントを、"Statua"Statusプロパティを発生させます。それらを最初に修正してみてください。

まだ動作しない場合は、変更しようとしているプロパティを指定してください。

+0

ありがとう。私はそのタイプミスを逃したとは信じられません。今のところ期待どおりに働いているようだ。 – Brian

0

エラーの原因が既に見つかりました。しかし、将来このような問題を避けるために、私はラムダ式を使用することをお勧めします。

それはしかし、いくつかのパフォーマンスの低下をもたらし、誰かがそれをここで説明していること注:http://blog.quantumbitdesigns.com/2010/01/26/mvvm-lambda-vs-inotifypropertychanged-vs-dependencyobject/

しかし、プロパティは、通知は通常、そうめったにこれは本当に問題ではないことにトリガされません変更しました。しかしそれはあなたの使い方に依存します...

関連する問題