簡體   English   中英

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

[英]Notify changes in list wpf .net4.0

我正在嘗試將項目廣告到如下所示的列表,並更新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}" />

當我按下按鈕我在列表中添加對象時,計數器會計數,但列表中沒有任何反應。

您應該使用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

你'對象'必須實現INotifyPropertyChanged ,而不是窗體或窗口。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM