C# bindinglist中的对象属性更改时datagrid单元格不更新
我有一个带有datagrid的WPF应用程序,其ItemsSource是一个BindingList。当我更改BindingList中的对象时,datagrid不会被更新。当我修改BindingList中的对象时,如何确保datagrid更新 以下是XAML:C# bindinglist中的对象属性更改时datagrid单元格不更新,c#,.net,wpf,datagrid,bindinglist,C#,.net,Wpf,Datagrid,Bindinglist,我有一个带有datagrid的WPF应用程序,其ItemsSource是一个BindingList。当我更改BindingList中的对象时,datagrid不会被更新。当我修改BindingList中的对象时,如何确保datagrid更新 以下是XAML: <DataGrid CanUserAddRows="False" AutoGenerateColumns="False" HorizontalAlignment="Stretch" Name="dgLocalPlugins" Vert
<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>
现在,当我修改一个PluginDescription对象的属性时,DataGrid并没有被更新。我认为在PluginDescription类上实现INotifyPropertyChange接口就是我所需要的。是否需要执行其他操作?必须为
PropertyChanged
事件提供属性的准确名称。在您的代码中,存在一些不匹配的情况:WikiUrl
属性使用“Wiki”
引发PropertyChanged
事件,而Status
属性使用“Statua”
。试着先把它们修好
如果仍然不起作用,请指定要更改的属性好吗?您已经找到了错误的原因。但为了避免将来出现此类问题,我建议在可接受的情况下使用Lambda表达式 请注意,这会带来一些性能损失,但有人在这里描述了这一点:
但属性更改通知通常很少触发,因此这并不是一个真正的问题。然而,这取决于你的使用 谢谢。真不敢相信我漏掉了那个打字错误。现在似乎正在按预期工作。
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;
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));
}
}
}