wpf 如何使用 xaml 将数据网格绑定到 collectionviewsource

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27227262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:45:37  来源:igfitidea点击:

how to bind datagrid to collectionviewsource using xaml

c#wpfxamldatagrid

提问by CoverG

I have a datagrid which is bound to a collectionviewsource, which is bound to an observablecollection. In following a guide I've set it up like so:

我有一个绑定到 collectionviewsource 的数据网格,它绑定到一个 observablecollection。在遵循指南时,我已将其设置如下:

My Persons class:

我的人物类:

public class Persons : ObservableCollection<Person>
{
    //...
}

The xaml data bindings:

xaml 数据绑定:

<Window.Resources>
    <local:Persons x:Key="_Persons"/>
    <CollectionViewSource x:Key="cvsPersons" Source="{StaticResource _Persons}" />                       
</Window.Resources>

The datagrid binding:

数据网格绑定:

 <DataGrid x:Name="myDataGrid" ItemsSource="{Binding Source={StaticResource cvsPersons}}"/>

The code behind:

背后的代码:

_Persons = (Persons)this.Resources["_Persons"];
_persons = //some method to fill perons;
cvsPersons = (CollectionViewSource)this.Resources["cvsPersons"];             
cvsPersons.Source = _Persons;

The above works. My question is, why do I need to set the collectionviewsource.source in code behind using cvsPersons.Source = _Persons;? I thought the xaml in my very first snippet did this job:

以上工作。 我的问题是,为什么我需要使用 cvsPersons.Source = _Persons; 在后面的代码中设置 collectionviewsource.source?我认为我的第一个片段中的 xaml 完成了这项工作:

_cvsPersons.Source = _Persons;  

If I need all this code behind then the xaml databinding code seems of little benefit, I may as well do everything in code behind. From my (perhaps little) understanding, the only code needed in code behind would be to reference the instances setup by xaml, ie:

如果我需要所有这些代码,那么 xaml 数据绑定代码似乎没什么好处,我也可以在代码背后做所有事情。根据我(可能很少)的理解,后面代码中唯一需要的代码是引用由 xaml 设置的实例,即:

_Persons = (Persons)this.Resources["_Persons"];
_persons = //some method to fill perons;
cvsPersons = (CollectionViewSource)this.Resources["cvsPersons"];

If I don't have _cvsPersons.Source = _Persons; then my datagrid does not get populated. My xaml as it stands doesn't do the job. I guess my question is more concept related..

如果我没有 _cvsPersons.Source = _Persons; 那么我的数据网格不会被填充。我的 xaml 无法完成这项工作。我想我的问题与概念更相关..

回答by SSchuette

To avoid your code behind approach you should use the MVVM-pattern MVVM Model View ViewModel . A possible solution could be a "Person" (acting as the Model) like this:

为了避免您的代码隐藏方法,您应该使用 MVVM 模式MVVM 模型视图 ViewModel。一个可能的解决方案可能是这样的“人”(充当模型):

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

The you could implement a ViewModel initializing a property with an ObservableCollection of Persons.

您可以实现一个 ViewModel,该模型使用一个 ObservableCollection of Persons 初始化一个属性。

public class ViewModel
{
    public ObservableCollection<Person> Persons { get; set; }

    public ViewModel()
    {
        Persons = new ObservableCollection<Person>();
    }
}

Your MainWindow.cs now has to initialize the ViewModel:

您的 MainWindow.cs 现在必须初始化 ViewModel:

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

It is important to set the DataContext to the ViewModel object. I added a Button an the method to add a Person.

将 DataContext 设置为 ViewModel 对象很重要。我添加了一个按钮和一个方法来添加一个人。

    private void AddPersonOnClick(object sender, RoutedEventArgs e)
    {
        ViewModel.Persons.Add(new Person
        {
            Age = 55,
            Name = "Sand"
        });
    }

Now you can instantiate an CollectionViewSource in XAML and bind it to the Persons ObservableCollection property in your ViewModel.

现在,您可以在 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>

Finally you have to set the ItemsSource as you posted it to the CollectionViewSource and it works like a charm.

最后,您必须在将 ItemsSource 发布到 CollectionViewSource 时对其进行设置,它的工作原理非常棒。

EDIT

编辑

I tried your solution and it's working as well. MainWindow.xaml:

我试过你的解决方案,它也有效。主窗口.xaml:

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

And it's important that you initialize your Persons Collection after the InitializeComponent(). MainWindow.cs

InitializeComponent()之后初始化Persons 集合很重要。主窗口.cs

        InitializeComponent();
        Persons persons = (Persons)this.FindResource("Persons");

        persons.Add(new Person
        {
            Age = 23,
            Name = "Dude"
        });

This solution works without code-behind constructs to set the ItemsSource.

此解决方案无需代码隐藏构造来设置 ItemsSource。