简体   繁体   English

如何将DataGrid绑定到数据源的第二级属性?

[英]How to bind DataGrid to a second-level property of a data source?

Suppose you write up code for a class Person whose properties are composed of primitive types and a class Address whose properties are all primitive types. 假设您为一个类Person编写代码,该类Person的属性由原始类型组成, 一个Address类则所有属性都是原始类型。 You put multiple Person objects in an ObservableCollection and you want to bind it to a DataGrid. 您将多个Person对象放入ObservableCollection并且想要将其绑定到DataGrid。 The properties with primitive types will display normally, but the property Address , that is a class composed of primitive types, will just display "(Collection)". 具有基本类型的属性将正常显示,但是由基本类型组成的类的属性Address将仅显示“(Collection)”。

I found a solution to this problem while googling, but it seems like a lot of work for a little functionality. 谷歌搜索时,我找到了解决该问题的方法,但是对于一些功能而言,这似乎需要大量工作。 The solution I found was for DataGridView and it was dated for 2007. Is there an easier way now that we can use WPF and DataGrid or is it just as difficult? 我找到的解决方案是针对DataGridView ,它的日期是2007年。现在有没有更简单的方法可以使用WPF和DataGrid还是一样困难?

Example code: 示例代码:

class Person
{
    private string id;
    private string name;
    private Address homeAddr;

    public string ID
    {
            get { return id;}
            set { id = value;}
    }

    public string Name
    {
            get { return name;}
            set { name = value;}
    }

    public Address HomeAddr
    {
            get { return homeAddr;}
            set { homeAddr = value;}
    }
}

class Address
{
    private string cityname;
    private string postcode;

    public string CityName
    {
        get { return cityname;}
        set { cityname = value;}
    }

    public string PostCode
    {
        get { return postcode;}
        set { postcode = value;}
    }
}
<DataGrid>
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Id}"/>
    <DataGridTextColumn Binding="{Binding Name}"/>
    <DataGridTextColumn Binding="{Binding HomeAddr.CityName}"/>
    <DataGridTextColumn Binding="{Binding HomeAddr.PostCode}"/>
  </DataGrid.Columns>
</DataGrid>

The reason why you can't add the Address directly to your DataGrid column is because that column expects a primitive type and you send an "Address" type object. 无法将Address直接添加到DataGrid列的原因是因为该列需要原始类型,并且您发送了“ Address”类型对象。 To fix this, you must create a Converter that transforms your Address object into a primitive type, like a string. 要解决此问题,您必须创建一个Converter,将您的Address对象转换为原始类型,例如字符串。

First add your converter in the Resource Dictionary 首先在资源字典中添加转换器

<src:AddressToStringConverter x:Key="AddressToStringConverter" />

and afterwards use it in your grid 然后在您的网格中使用它

<DataGrid>
  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding Id }"/>
    <DataGridTextColumn Binding="{Binding Name}"/>
    <DataGridTextColumn Binding="{Binding HomeAddr, Converter={StaticResource AddressToStringConverter}}"/>
  </DataGrid.Columns>
</DataGrid>

Here you can find out more about converters : http://wpftutorial.net/ValueConverters.html 在这里,您可以找到有关转换器的更多信息: http : //wpftutorial.net/ValueConverters.html

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

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