簡體   English   中英

如何使用xaml將datagrid綁定到collectionviewsource

[英]how to bind datagrid to collectionviewsource using xaml

我有一個datagrid綁定到一個collectionviewsource,它綁定到一個observablecollection。 在以下指南中,我將其設置為:

我的人類:

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

xaml數據綁定:

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

datagrid綁定:

 <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;? 我認為我的第一個片段中的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不能完成這項工作。 我想我的問題更多是與概念相關的..

為避免代碼隱藏,您應該使用MVVM模式MVVM模型視圖ViewModel 一個可能的解決方案可能是“人”(充當模型),如下所示:

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

您可以使用ObservableCollection of Persons實現一個ViewModel初始化屬性。

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對象非常重要。 我添加了一個Button來添加Person的方法。

    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 Collection非常重要。 MainWindow.cs

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

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

此解決方案無需代碼隱藏構造即可設置ItemsSource。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM