【问题标题】:Notify changes in list wpf .net4.0通知列表 wpf .net4.0 中的更改
【发布时间】:2011-06-05 04:23:43
【问题描述】:

我正在尝试将项目添加到如下所示的列表中,并更新 DataGrid,但我似乎遗漏了一些东西,我应该通知哪个属性?

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}" />

当我按下按钮时,我在列表中添加对象,计数器计数但列表中没有任何反应。

【问题讨论】:

    标签: c# xaml datagrid .net-4.0 inotifypropertychanged


    【解决方案1】:

    你应该使用 ObservableCollection 而不是 List

    代码应该是-

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

    INotifyPropertyChanged 也应该在不在窗口中的对象上实现

    【讨论】:

      【解决方案2】:

      您的“对象”必须实现INotifyPropertyChanged,而不是表单或窗口。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-31
        • 1970-01-01
        • 2011-02-20
        • 1970-01-01
        • 2012-10-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多