C# WPF数据网格绑定混乱
我对WPF数据绑定有点困惑。我试过很多例子,但我认为我不了解这个主题的基本知识 我有以下datagrid,绑定到一个ObservableCollection(Of T),其中T类有一个Name属性,该属性显示在datagrid列中。My T类还实现INotifyPropertyChanged,并在Name属性更改时正确激发事件C# WPF数据网格绑定混乱,c#,wpf,xaml,binding,datagrid,C#,Wpf,Xaml,Binding,Datagrid,我对WPF数据绑定有点困惑。我试过很多例子,但我认为我不了解这个主题的基本知识 我有以下datagrid,绑定到一个ObservableCollection(Of T),其中T类有一个Name属性,该属性显示在datagrid列中。My T类还实现INotifyPropertyChanged,并在Name属性更改时正确激发事件 <DataGrid Grid.Row="1" Name="MyDataGrid" AutoGenerateColumns="False" ItemsSource="
<DataGrid Grid.Row="1" Name="MyDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding}" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="NameColumn" Header="Name" Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
这一切正常(数据显示正确)。但是,如果我重新加载集合(再次从某处获取完全不同的数据),如果我不再执行MyDataGrid.ItemsSource=MyCollection;指令,则不会更新数据网格
我认为每次重新加载数据时使用XXX.ItemsSource=YYY不是一个好的做法,因此我猜我做错了什么。在一些示例中,我看到XAML DataGrid的绑定方式如下:
<DataGrid ItemsSource="{Binding CollectionName}">
...
</DataGrid>
...
我猜这是为了使用该集合,所以不需要以编程方式执行.ItemsSource。。。但我无法让它运行
有人能告诉我隧道尽头的灯吗?您可以数据绑定DataGrid的
项资源,而不是手动分配它。只需将MyCollection
声明为公共属性,正确发出PropertyChanged
通知,并为您的DataGrid
设置DataContext
(或为DataGrid所在的窗口设置DataContext
):
理论上(因为我没有在这个问题中专门尝试过这个场景),使用ItemsSource
绑定,每当集合重新加载时,您的DataGrid将自动更新,当设置项目时,手动设置资源不能产生相同的效果。您没有解释最重要的部分,即如何实现填充?如果通过将新集合引用指定给旧变量来执行此操作,则此操作将不起作用,因为集合是引用类型。任何解决问题的方法都可以执行以下两种操作之一(除了解决方案之外):
第一种方法是将ObservableCollection定义为窗口中的依赖项属性,然后可以使用它。
第二个也是最简单的方法是清除集合,然后使用源集合中的foreach循环添加新项
以下是第一种情况的示例:
public static readonly DependencyProperty MyCollectionProperty = DependencyProperty.Register("MyCollection", typeof(ObservableCollection<T>), typeof(MainWindow));
public ObservableCollection<T> MyCollection
{
get
{
return this.GetValue(MyCollectionProperty) as string;
}
set
{
this.SetValue(MyCollectionProperty, value);
}
}
//assign the collection value at any time
MyCollection = ......
//you can bind it as the past
public static readonly dependencProperty MyCollectionProperty=dependencProperty.Register(“MyCollection”、typeof(observedcollection)、typeof(main window));
公共可观测集合MyCollection
{
得到
{
以字符串形式返回此.GetValue(MyCollectionProperty);
}
设置
{
this.SetValue(MyCollectionProperty,value);
}
}
//随时指定集合值
MyCollection=。。。。。。
//你可以把它当作过去
您必须实现INotifyPropertyChanged才能引发PropertyChanged事件。如果您使用的是ObservableCollection,那么就不必费心引发事件。只需放置MyCollection=新的ObservableCollection();在构造函数中,然后将InitApp方法中加载的项添加到MyCollection。这一切都是在假设您在数据网格中使用以下绑定的情况下进行的:ItemsSource=“{binding MyCollection}”
<DataGrid ItemsSource="{Binding CollectionName}">
...
</DataGrid>
private ObservableCollection<MyClass> _myCollection
public ObservableCollection<MyClass> MyCollection
{
get { return _myCollection; }
set
{
_myCollection = value;
NotifyPropertyChanged("MyCollection");
}
}
public void InitApp() {
MyCollection = [... taking data from somewhere ...];
MyDataGrid.DataContext = this;
}
<DataGrid ItemsSource="{Binding MyCollection}">
...
</DataGrid>
<DataGrid Grid.Row="1" Name="MyDataGrid"
AutoGenerateColumns="False"
ItemsSource="{Binding}" >
public void InitApp() {
MyCollection = [... taking data from somewhere ...];
MyDataGrid.DataContext = MyCollection;
}
public static readonly DependencyProperty MyCollectionProperty = DependencyProperty.Register("MyCollection", typeof(ObservableCollection<T>), typeof(MainWindow));
public ObservableCollection<T> MyCollection
{
get
{
return this.GetValue(MyCollectionProperty) as string;
}
set
{
this.SetValue(MyCollectionProperty, value);
}
}
//assign the collection value at any time
MyCollection = ......
//you can bind it as the past