C# 通知列表wpf.net4.0中的更改
我正试图将项目添加到如下所示的列表中,并更新DataGrid,但似乎我缺少了一些东西,我应该通知哪个属性 C#C# 通知列表wpf.net4.0中的更改,c#,xaml,datagrid,.net-4.0,inotifypropertychanged,C#,Xaml,Datagrid,.net 4.0,Inotifypropertychanged,我正试图将项目添加到如下所示的列表中,并更新DataGrid,但似乎我缺少了一些东西,我应该通知哪个属性 C# 公共部分类窗口8:INotifyPropertyChanged { 公共TestObj TestObject{get;set;} 公共整数计数{get;set;} 公共窗口8() { 初始化组件(); DataContext=this; var newList=新列表(); Add(新测试{I=1,S=“Test”}); TestObject=newtestobj{S=“Testing
公共部分类窗口8:INotifyPropertyChanged
{
公共TestObj TestObject{get;set;}
公共整数计数{get;set;}
公共窗口8()
{
初始化组件();
DataContext=this;
var newList=新列表();
Add(新测试{I=1,S=“Test”});
TestObject=newtestobj{S=“Testing object”,List=newList};
Count=TestObject.List.Count;
}
专用ICommand_addItemCommand;
公共ICommand AddItemCommand
{
得到
{
如果(_addItemCommand==null)
_addItemCommand=新的RelayCommand(n=>
{
Add(新测试{I=1,S=“newobject”});
Count=TestObject.List.Count;
NotifyPropertyChanged(“TestObject”);
//NotifyPropertyChanged(“TestObject.List”);
NotifyPropertyChanged(“计数”);
});
返回_addItemCommand;
}
}
公共事件属性更改事件处理程序属性更改;
受保护的void NotifyPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
公共类TestObj
{
公共字符串S{get;set;}
公共列表{get;set;}
}
公开课考试
{
公共字符串S{get;set;}
公共整数I{get;set;}
}
XAML
添加项
当我按下按钮在列表中添加对象时,计数器计数,但列表中什么也没有发生。
您的“对象”必须实现
INotifyPropertyChanged
,而不是表单或窗口。您的“对象”必须实现INotifyPropertyChanged
,而不是表单或窗口。您应该使用ObservableCollection而不是列表
代码应该是-
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; }
}
公共窗口8()
{
初始化组件();
DataContext=this;
var newList=newobserveCollection();
Add(新测试{I=1,S=“Test”});
TestObject=newtestobj{S=“Testing object”,List=newList};
Count=TestObject.List.Count;
}
公共类TestObj
{
公共字符串S{get;set;}
公共ObservableCollection列表{get;set;}
}
此外,INotifyPropertyChanged应该在不在窗口中的对象上实现您应该使用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; }
}
公共窗口8()
{
初始化组件();
DataContext=this;
var newList=newobserveCollection();
Add(新测试{I=1,S=“Test”});
TestObject=newtestobj{S=“Testing object”,List=newList};
Count=TestObject.List.Count;
}
公共类TestObj
{
公共字符串S{get;set;}
公共ObservableCollection列表{get;set;}
}
此外,应在不在窗口中的对象上实现INotifyPropertyChanged
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; }
}