简体   繁体   English

通知列表wpf .net4.0中的更改

[英]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? 我正在尝试将项目广告到如下所示的列表,并更新DataGrid但似乎我遗漏了一些东西,我应该通知哪个属性?

C# 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 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. 当我按下按钮我在列表中添加对象时,计数器会计数,但列表中没有任何反应。

You should use an ObservableCollection instead of a List 您应该使用ObservableCollection而不是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 还应在不在窗口中的对象上实现INotifyPropertyChanged

你'对象'必须实现INotifyPropertyChanged ,而不是窗体或窗口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM