简体   繁体   English

具有复杂项目的列表的数据绑定

[英]Data binding of a list with complex items

I'm trying to bind a list of type User into a data grid with 2 columns. 我正在尝试将类型为User列表绑定到具有2列的数据网格中。

class User: 类用户:

public class User
    {

        private string username;
        private string password;
        private Object person;

        public User(string _username, string _password, Object _person=null)
        {
            username = _username;
            password = _password;
                person = _person;
        }

        public string UserName
        {
            get { return username; }
            set { username = value; }
        }
        public string Password
        {
            get { return password; }
            set { password = value; }
        }
        public Object Person
        {
            get { return person; }
            set { person = value; }
        }

main.xaml: main.xaml:

<DataGrid Name="UserGrid" ItemsSource="{Binding}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="User name" Binding="{Binding UserName}"/>
            <DataGridTextColumn Header="Person" Binding="{Binding Person}"/>
       </DataGrid.Columns>
</DataGrid>

main.xaml.cs main.xaml.cs

UserGrid.DataContext = listOfUsers;

This code works fine! 此代码可以正常工作! it binds a list of Users into the DataGrid. 它将用户列表绑定到DataGrid中。 Now it's displaying the value of username and the value of person. 现在,它显示用户名的值和人员的值。

But i want to display an internal property of the Object person . 但是我想展示对象人内部属性 Let's say that the object that called person is of type Food and has a property called taste . 假设被称为person的对象是Food类型的,并且具有名为aste的属性。

How do i make the DataGrid display the value of username and the value of Person.taste ? 我如何使DataGrid显示用户名和Person.taste的值?

Depending on how you want to display the data, here are a few different things you can do (this isn't an exhaustive list): 根据您想要显示数据的方式,可以执行以下几项操作(这不是详尽的列表):

  1. Bind to Person.Food.taste directly. 直接绑定到Person.Food.taste
  2. Add a ToString() override to the Person class and return the string the way you want it to be displayed. ToString()重写添加到Person类,然后以希望显示的方式返回字符串。 You won't be able to edit it this way without doing extra work, but not sure if editing is a requirement. 如果不做额外的工作,您将无法以这种方式进行编辑,但是不确定是否需要编辑。
  3. Use a DataGridTemplateColumn to present the data in any custom way that you want to (and handle your own editing if needed). 使用DataGridTemplateColumn以所需的任何自定义方式显示数据(并在需要时进行自己的编辑)。 For example: 例如:

     <DataGridTemplateColumn Header="Person"> <DataGridTemplateColumn.CellTemplate> <DataTemplate DataType="{x:Type local:User}"> <!-- Put any template you want here --> <TextBlock Text="{Binding Person.Food.taste}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 

Edit: Based on your follow-up, if you want to have a column display different data for different types of objects, then you will need to use the DataGridTemplateColumn I mentioned, setup up a DataTemplate for each type of data, and create a template selector to select the correct one. 编辑:根据您的后续操作,如果您想让一列为不同类型的对象显示不同的数据,那么您将需要使用我提到的DataGridTemplateColumn ,为每种数据类型设置一个DataTemplate ,并创建一个模板选择器以选择正确的一个。 So, your column will look something like this: 因此,您的专栏将如下所示:

<DataGridTemplateColumn Header="Rank">
    <DataGridTemplateColumn.CellTemplateSelector>
        <local:RankTemplateSelector>
            <local:RankTemplateSelector.EmployeeTemplate>
                <DataTemplate DataType="{x:Type local:Employee}">
                    <TextBlock Text="{Binding Rank}" />
                </DataTemplate>
            </local:RankTemplateSelector.EmployeeTemplate>
            <local:RankTemplateSelector.CustomerTemplate>
                <DataTemplate DataType="{x:Type local:Customer}">
                    <TextBlock Text="N/A" />
                </DataTemplate>
            </local:RankTemplateSelector.CustomerTemplate>
            <local:RankTemplateSelector.ClubMemberTemplate>
                <DataTemplate DataType="{x:Type local:ClubMember}">
                    <TextBlock Text="N/A" />
                </DataTemplate>
            </local:RankTemplateSelector.ClubMemberTemplate>
        </local:RankTemplateSelector>
    </DataGridTemplateColumn.CellTemplateSelector>
</DataGridTemplateColumn>

And your template selector will look something like this: 您的模板选择器将如下所示:

class RankTemplateSelector : DataTemplateSelector
{
    public DataTemplate EmployeeTemplate { get; set; }
    public DataTemplate CustomerTemplate { get; set; }
    public DataTemplate ClubMemberTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        if (item == null) return null;
        if (item is Employee) return EmployeeTemplate;
        if (item is Customer) return CustomerTemplate;
        if (item is ClubMember) return ClubMemberTemplate;
        throw new ArgumentException("The type of the item is not recognized", "item");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM