C# 如何使用xaml将datagrid绑定到collectionviewsource
我有一个绑定到collectionviewsource的datagrid,它绑定到observablecollection。在以下指南中,我将其设置为: 我的个人课:C# 如何使用xaml将datagrid绑定到collectionviewsource,c#,wpf,xaml,datagrid,C#,Wpf,Xaml,Datagrid,我有一个绑定到collectionviewsource的datagrid,它绑定到observablecollection。在以下指南中,我将其设置为: 我的个人课: public class Persons : ObservableCollection<Person> { //... } 以上工作我的问题是,为什么需要使用cvsPersons.source=\u Persons;在代码隐藏中设置collectionviewsource.source;?我认为我的第一个代码
public class Persons : ObservableCollection<Person>
{
//...
}
以上工作我的问题是,为什么需要使用cvsPersons.source=\u Persons;在代码隐藏中设置collectionviewsource.source;?我认为我的第一个代码片段中的xaml完成了这项工作:
_cvsPersons.Source = _Persons;
如果我需要所有这些代码,那么xaml数据绑定代码似乎没有什么好处,我也可以使用代码隐藏来完成所有工作。根据我的理解(可能很少),代码隐藏中唯一需要的代码是引用xaml设置的实例,即:
_Persons = (Persons)this.Resources["_Persons"];
_persons = //some method to fill perons;
cvsPersons = (CollectionViewSource)this.Resources["cvsPersons"];
如果我没有_cvsPersons.Source=_Persons;然后我的datagrid不会被填充。我的xaml目前无法完成这项工作。我想我的问题与概念更相关。为了避免代码隐藏方法,您应该使用MVVM模式。一个可能的解决方案可以是这样的“人”(充当模型):
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
您可以实现一个ViewModel,用可观察的人员集合初始化属性
public class ViewModel
{
public ObservableCollection<Person> Persons { get; set; }
public ViewModel()
{
Persons = new ObservableCollection<Person>();
}
}
将DataContext设置为ViewModel对象非常重要。我在添加人员的方法中添加了一个按钮
private void AddPersonOnClick(object sender, RoutedEventArgs e)
{
ViewModel.Persons.Add(new Person
{
Age = 55,
Name = "Sand"
});
}
现在,您可以在XAML中实例化CollectionViewSource,并将其绑定到ViewModel中的Persons ObservableCollection属性
<Window x:Class="DataGridStackoverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<CollectionViewSource x:Key="PersonsCollectionViewSource" Source="{Binding Persons}" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" ItemsSource="{Binding Source={StaticResource PersonsCollectionViewSource}}" />
<Button x:Name="AddPerson" Grid.Row="1" Click="AddPersonOnClick" HorizontalAlignment="Left">Add Person</Button>
</Grid>
此解决方案不需要代码隐藏结构来设置ItemsSource。非常感谢。让它工作起来。为MVVM上的方法干杯。
public class ViewModel
{
public ObservableCollection<Person> Persons { get; set; }
public ViewModel()
{
Persons = new ObservableCollection<Person>();
}
}
public partial class MainWindow : Window
{
public ViewModel ViewModel;
public MainWindow()
{
ViewModel = new ViewModel();
ViewModel.Persons.Add(new Person
{
Age = 29,
Name = "Mustermann"
});
ViewModel.Persons.Add(new Person
{
Age = 35,
Name = "Meyer"
});
this.DataContext = ViewModel;
InitializeComponent();
}
private void AddPersonOnClick(object sender, RoutedEventArgs e)
{
ViewModel.Persons.Add(new Person
{
Age = 55,
Name = "Sand"
});
}
<Window x:Class="DataGridStackoverflow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<CollectionViewSource x:Key="PersonsCollectionViewSource" Source="{Binding Persons}" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" ItemsSource="{Binding Source={StaticResource PersonsCollectionViewSource}}" />
<Button x:Name="AddPerson" Grid.Row="1" Click="AddPersonOnClick" HorizontalAlignment="Left">Add Person</Button>
</Grid>
<Window.Resources>
<dataGridStackoverflow:Persons x:Key="Persons" />
<CollectionViewSource x:Key="PersonsCollectionViewSource" Source="{StaticResource Persons}" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<DataGrid Grid.Row="0" ItemsSource="{Binding Source={StaticResource PersonsCollectionViewSource}}" />
<Button x:Name="AddPerson" Grid.Row="1" Click="AddPersonOnClick" HorizontalAlignment="Left">Add Person</Button>
</Grid>
InitializeComponent();
Persons persons = (Persons)this.FindResource("Persons");
persons.Add(new Person
{
Age = 23,
Name = "Dude"
});