puti26
puti26

Reputation: 429

WPF Datagrid NotifyOfPropertyChange doesn't work

i have a view with two datagrid and one button. enter image description here The first datagrid contains a list of articles, the second is empty and the user after press a button will add in this datagrid the article selected. I add Caliburn.Micro in this project for use the "Screen". The problem is that also if i press add article button, nothing changed and the datagrid is always empty. This is the XAML code:

<DataGrid temsSource="{Binding Articles}"
     AutoGenerateColumns="False" HorizontalAlignment="Left" CanUserAddRows="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
        <DataGridTextColumn Header="Price" Binding="{Binding Price}" />
    </DataGrid.Columns>
</DataGrid>

And this the ViewModel code:

public class ShellViewModel : Screen
{
    public List<Article> _articles;
    public List<Article> Articles
    {
        get { return _articles; }
        set


{
        if (value != _articles)
        {
            _articles = value;
            NotifyOfPropertyChange("Articles");
        }
    }
}


public void AddArticle()
{            
    Articles.Add(new Article 
    { 
        Id = ArticleSelected.Id,
        Name = ArticleSelected.Name,
        Price = ArticleSelected.Price,
    });

    NotifyOfPropertyChange("Articles");            
}   

}

Where I wrong ?

Upvotes: 0

Views: 742

Answers (2)

pushpraj
pushpraj

Reputation: 13669

the Issue is not because of property notification but the change of collection is not notified so perhaps using ObservableCollection<T> will solve your issue

public ObservableCollection<Article> _articles;
public ObservableCollection<Article> Articles
{
....

Upvotes: 3

Sajeetharan
Sajeetharan

Reputation: 222582

Change your List<Article> _articles to ObservableCollectiont<Article> _articles , If you use ObservableCollection, then whenever you modify the list, it will raise the CollectionChanged event - an event that will tell the WPF binding to update. Check INotifyCollectionChanged

public ObservableCollection<Article> _articles;
public ObservableCollection<Article> Articles

Upvotes: 2

Related Questions