我有一個WPF應用程序,其中的ItemsSource是一個BindingList。當我對BindingList中的對象進行更改時,數據網格未被更新。當我修改BindingList中的對象時,如何確保datagrid更新?當綁定列表中的對象屬性發生變化時,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對象的屬性時,DataGrid沒有被更新。我認爲在PluginDescription類上實現INotifyPropertyChange接口是我所需要的。我需要做其他事嗎?
謝謝。不能相信我錯過了那個錯字。似乎現在按預期工作。 – Brian