Cinaird
Cinaird

Reputation: 785

Notify changes in list wpf .net4.0

I'm trying to ad item to a list like seen below, and update the DataGrid but it seem i'm missing something, which property should I notify?

C#

public partial class Window8 : INotifyPropertyChanged
    {
        public TestObj TestObject { get; set; }
        public int Count { get; set; }

        public Window8()
        {
            InitializeComponent();
            DataContext = this;

            var newList = new List<Test>();

            newList.Add(new Test{I = 1, S = "Test"});
            TestObject = new TestObj { S = "Testing object", List = newList };

            Count = TestObject.List.Count;
        }


        private ICommand _addItemCommand;
        public ICommand AddItemCommand
        {
            get
            {
                if (_addItemCommand == null)
                    _addItemCommand = new RelayCommand(n =>
                                                           {
                                                               TestObject.List.Add(new Test {I = 1, S = "New object"});
                                                               Count = TestObject.List.Count;

                                                               NotifyPropertyChanged("TestObject");
                                                               //NotifyPropertyChanged("TestObject.List");

                                                               NotifyPropertyChanged("Count");

                                                           });
                return _addItemCommand;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;


        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public class TestObj
    {
        public string S { get; set; }
        public List<Test> List { get; set; }
    }

    public class Test
    {
        public string S { get; set; }
        public int I { get; set; }
    }

XAML

<StackPanel>
        <Button Height="23" Width="100" Command="{Binding Path=AddItemCommand}" >Add Item</Button>
        <DataGrid Height="100" ItemsSource="{Binding Path=TestObject.List}" IsReadOnly="True" />
 <TextBlock Text="{Binding Path=Count}" />

When i press the button i add objects in the list, the counter counts up but nothing happens in the list.

Upvotes: 2

Views: 3233

Answers (2)

Jason Quinn
Jason Quinn

Reputation: 2563

You should use an ObservableCollection instead of a List

Code Should be-

public Window8()
{
    InitializeComponent();
    DataContext = this;

    var newList = new ObservableCollection<Test>();

    newList.Add(new Test { I = 1, S = "Test" });
    TestObject = new TestObj { S = "Testing object", List = newList };

    Count = TestObject.List.Count;
}

public class TestObj
{

    public string S { get; set; }
    public ObservableCollection<Test> List { get; set; }
}

Also INotifyPropertyChanged should be implemented on the Objects not in the Window

Upvotes: 3

leppie
leppie

Reputation: 117240

You 'objects' must implement INotifyPropertyChanged, not the form or window.

Upvotes: 1

Related Questions