当绑定列表中的对象属性更改时,datagrid单元格不会更新

时间:2012-01-21 19:02:33

标签: c# .net wpf datagrid bindinglist

我有一个带有datagrid的WPF应用程序,其ItemsSource是一个BindingList。当我对BindingList中的对象进行更改时,数据网格没有被更新。当我修改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对象的属性时,DataGrid没有被更新。我认为在PluginDescription类上实现INotifyPropertyChange接口将是我所需要的。我还需要做点什么吗?

2 个答案:

答案 0 :(得分:1)

必须为PropertyChanged事件提供您的媒体资源的确切名称。在您的代码中,您有一些不匹配:WikiUrl属性使用PropertyChanged引发"Wiki"个事件,使用Status引发"Statua"属性。先尝试修复它们。

如果仍然无效,请指定您要更改的属性?

答案 1 :(得分:0)

您已找到错误原因。但为了避免将来出现此类问题,我建议在可接受的情况下使用Lambda表达式。

请注意,它会带来一些性能损失,有人在这里描述:http://blog.quantumbitdesigns.com/2010/01/26/mvvm-lambda-vs-inotifypropertychanged-vs-dependencyobject/

但是,通常很少触发属性更改通知,这不是真正的问题。但这取决于您的使用情况......