0

I have a WPF application with a datagrid who's ItemsSource is a BindingList. When I make changes to objects in the BindingList the datagrid is not being updated. How can I make sure the datagrid updates when I modify the object in the BindingList?

Here is the 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>

Here is the relevant code behind where I populate the 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;

Here is the PluginDescription class:

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));
        }
    }

}

Now, when I modify a property of one of the PluginDescription objects the DataGrid is not being updated. I thought implementing the INotifyPropertyChange interface on the PluginDescription class would be all I need. Do I need to do something else?

2 Answers 2

1

The PropertyChanged event must be supplied with the precise name of your property. In your code, you have some mismatches: the WikiUrl property raises a PropertyChanged event with "Wiki", and the Status property with "Statua". Try fixing those first.

If it still doesn’t work, could you please specify which properties you’re trying to change?

1
  • Thanks. Can't believe I missed that typo. Seems to be working as expected now.
    – Brian
    Jan 21, 2012 at 19:23
0

You already found the cause of the error. But to avoid such issues in the future I suggest using Lambda Expressions where acceptable.

Note that it brings some performance penalty though, somebody described it here: http://blog.quantumbitdesigns.com/2010/01/26/mvvm-lambda-vs-inotifypropertychanged-vs-dependencyobject/

But property changed notifications are usually so rarely triggered that this is not really a problem. However it depends on your usage ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.