【问题标题】:how to bind datagrid to collectionviewsource using xaml如何使用 xaml 将数据网格绑定到 collectionviewsource
【发布时间】:2015-01-29 09:24:04
【问题描述】:

我有一个绑定到collectionviewsource 的datagrid,它绑定到observablecollection。在遵循指南时,我已将其设置为:

我的 Persons 类:

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

xaml 数据绑定:

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

数据网格绑定:

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

背后的代码:

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

以上工作。 我的问题是,为什么我需要在后面的代码中使用 cvsPersons.Source = _Persons; 来设置 collectionviewsource.source?我认为我的第一个 sn-p 中的 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;那么我的数据网格不会被填充。我的 xaml 不能胜任这项工作。我想我的问题与概念有关..

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

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

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

    您可以实现一个 ViewModel,使用 ObservableCollection 的 Persons 初始化一个属性。

    public class ViewModel
    {
        public ObservableCollection<Person> Persons { get; set; }
    
        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 对象很重要。我添加了一个按钮和添加人员的方法。

        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 发布到 CollectionViewSource 时设置它,它的工作原理就像一个魅力。

    编辑

    我尝试了您的解决方案,它也可以正常工作。 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>
    

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

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

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

    【讨论】:

    • 非常感谢。得到它的工作。也为 MVVM 上的方法喝彩。
    猜你喜欢
    • 2017-01-23
    • 2013-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多