如何使用xaml将datagrid绑定到collectionvi

如何使用xaml将datagrid绑定到collectionvi

本文介绍了如何使用xaml将datagrid绑定到collectionviewsource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagrid绑定到一个collectionviewsource,它绑定到一个observablecollection。在以下指南中,我已经设置好了:



我的Person类:

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

xaml数据绑定:

 < Window.Resources> 
< local:Persons x:Key =_ Persons/>
< CollectionViewSource x:Key =cvsPersonsSource ={StaticResource _Persons}/>
< /Window.Resources>

datagrid绑定:

 < DataGrid x:Name =myDataGridItemsSource ={Binding Source = {StaticResource cvsPersons}}/> 

后面的代码:

  _Persons =(Persons)this.Resources [_ Persons]; 
_persons = //一些方法来填充perons;
cvsPersons =(CollectionViewSource)this.Resources [cvsPersons];
cvsPersons.Source = _Persons;

以上工作。 我的问题是,为什么我需要使用cvsPersons.Source = _Persons来设置collectionviews源代码?我以为我的第一个片段中的xaml做了这个工作:

  _cvsPersons.Source = _Persons; 

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

  _Persons =(Persons)this.Resources [_ Persons]; 
_persons = //一些方法来填充perons;
cvsPersons =(CollectionViewSource)this.Resources [cvsPersons];

如果我没有_cvsPersons.Source = _Persons;那么我的数据网格没有被填充。我的xaml就不行了。我想我的问题是更多的概念相关..

解决方案

为了避免你的代码背后的方法,你应该使用MVVM模式 MVVM模型视图ViewModel 。一个可能的解决方案可能是这样的Person(作为模型):

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

您可以使用ObservableCollection of Persons来实现ViewModel初始化属性。

  public class ViewModel 
{
public ObservableCollection< Person>人{get;组; }

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

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

将DataContext设置为ViewModel对象很重要。我添加了一个Button来添加一个Person的方法。

  private void AddPersonOnClick(object sender,RoutedEventArgs e)
{
ViewModel.Persons.Add人
{
年龄= 55,
名称=沙
});
}

现在您可以在XAML中实例化一个CollectionViewSource,并将其绑定到Persons ObservableCollection属性在您的ViewModel中。

 < Window x:Class =DataGridStackoverflow.MainWindow
xmlns =http: /schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
标题=MainWindow高度=350Width =525>
< Window.Resources>
< CollectionViewSource x:Key =PersonsCollectionViewSourceSource ={Binding Persons}/>
< /Window.Resources>

< Grid>
< Grid.RowDefinitions>
< RowDefinition Height =*/>
< RowDefinition Height =Auto/>
< /Grid.RowDefinitions>

< DataGrid Grid.Row =0ItemsSource ={Binding Source = {StaticResource PersonsCollectionViewSource}}/>
< Button x:Name =AddPersonGrid.Row =1Click =AddPersonOnClickHorizo​​ntalAlignment =Left>添加人< / Button>
< / Grid>


最后你必须设置ItemsSource,当您发布到CollectionViewSource,它的作用就像一个魅力。



编辑



我尝试过你的解决方案,它也是有效的。 MainWindow.xaml:

 < Window.Resources> 
< dataGridStackoverflow:Persons x:Key =Persons/>
< CollectionViewSource x:Key =PersonsCollectionViewSourceSource ={StaticResource Persons}/>
< /Window.Resources>

< Grid>
< Grid.RowDefinitions>
< RowDefinition Height =*/>
< RowDefinition Height =Auto/>
< /Grid.RowDefinitions>

< DataGrid Grid.Row =0ItemsSource ={Binding Source = {StaticResource PersonsCollectionViewSource}}/>
< Button x:Name =AddPersonGrid.Row =1Click =AddPersonOnClickHorizo​​ntalAlignment =Left>添加人< / Button>
< / Grid>

重要的是,在InitializeComponent()之后初始化您的人物集合。 MainWindow.cs

  InitializeComponent(); 
人员=(人)this.FindResource(Persons);

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

此解决方案没有代码隐藏结构可以设置ItemsSource。


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:

My Persons class:

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

The xaml data bindings:

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

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:

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

解决方案

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:

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.

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

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

Your MainWindow.cs now has to initialize the 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.

    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.

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

EDIT

I tried your solution and it's working as well. MainWindow.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 persons = (Persons)this.FindResource("Persons");

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

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

这篇关于如何使用xaml将datagrid绑定到collectionviewsource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:10