简体   繁体   English

在WPF中绑定模型的通用属性

[英]binding a generic property of a model in wpf

I have a wpf usercontrol which has binding to a model(CustomerTaxModel) and it has a listview and GridView which should display data of property of type "T" Assets below. 我有一个wpf用户控件,它已绑定到模型(CustomerTaxModel),并且它具有一个listview和GridView,应在下面显示“ T”资产类型的属性数据。 This type T is a class with only properties and it has variable number of properties depending on the data we receive from third party tax verification company. 类型T是仅具有属性的类,它具有可变数量的属性,具体取决于我们从第三方税务验证公司收到的数据。

public class CustomerTaxModel

{

    public int ID { get; set; }

    public string Name { get; set; }

    public T Assets { get; set; }


}

My Generic type "T" classes looks like below. 我的通用类型“ T”类如下所示。 They may not have matching properties. 它们可能没有匹配的属性。 I should display data of this type T with property names in one column and their values in second column. 我应该在第一列中显示T类型的数据,并带有属性名称,在第二列中显示它们的值。 Is that possible to do this type of binding in xaml ? 可以在xaml中进行这种类型的绑定吗? The type T takes CorporateAsset , AgricultureReturns etc.. T类型采用CorporateAsset,AgricultureReturns等。

public class CorporateAsset
{        

    public string CorporateOfficeName { get; set; }

    public string NetWorth { get; set; }

    public string Subsidized { get; set; }

    public string LaunchDate{ get; set; }

}


 public class AgricultureReturns

{

    public string Name { get; set; }

    public string FieldSizeinAcres { get; set; }

    public string CropType { get; set; }

    public int NoOfSeasons { get; set; }

}

One solution I tried was binding to a KeyValuePair variable instead of binding the type "T" directly which works. 我尝试的一种解决方案是绑定到KeyValuePair变量,而不是直接绑定有效的“ T”类型。 .I referred the link and I tried with static property class in place of type T and it shows the data dynamically but when property itself is generic how can we display its properties in grid view. 我引用了链接,并尝试使用静态属性类代替T类型,它动态地显示数据,但是当属性本身是通用的时,如何在网格视图中显示其属性。

<UserControl x:Class="WpfApplication1.UserControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:WpfApplication1="clr-namespace:WpfApplication1" 
    mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    d:DesignHeight="189" d:DesignWidth="312" Width="300" Height="300">
<UserControl.Resources>
    <WpfApplication1:ConfigToDynamicGridViewConverter x:Key="ConfigToDynamicGridViewConverter" />
</UserControl.Resources>
<ListView ItemsSource="{Binding DataClass}" View="{Binding ColumnConfig, Converter={StaticResource ConfigToDynamicGridViewConverter}}"/>    

Binding.Path works by reflection. Binding.Path通过反射起作用。 It can look up any public instance property the actual runtime object has. 它可以查找实际运行时对象具有的任何公共实例属性。 This is how the DataContext property of WPF controls can be of type Object . 这就是WPF控件的DataContext属性可以是Object类型的方式。 Your Assets property could be object as well as far as WPF is concerned, but there's no reason to forgo strong typing in your models. 就WPF而言,您的Assets属性可以是对象,但没有理由放弃模型中的强类型。 WPF eats anything. WPF吃任何东西。

This gives me a TextBlock that says "Fred": 这给了我一个TextBlock ,上面写着“ Fred”:

public MainWindow()
{
    InitializeComponent();

    DataContext = new CustomerTaxModel<AgricultureReturns>()
    {
        Assets = new AgricultureReturns() { Name = "Fred" }
    };

}

XAML: XAML:

<TextBlock Text="{Binding Assets.Name}" />

If you've got an ObservableCollection of CustomerTaxModel , a common pattern is to have a bunch of DataTemplates for the different types that Assets might be. 如果您有一个CustomerTaxModelObservableCollection ,则常见的模式是为Assets可能具有的不同类型提供一堆DataTemplates These are pretty lame templates, but you get the idea. 这些是非常糟糕的模板,但是您可以理解。

<ItemsControl ItemsSource="{Binding TaxModels}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" />
                <!-- DataTemplates in Resources will be used here automagically -->
                <ContentControl Content="{Binding Assets}" />
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.Resources>
        <DataTemplate TargetType="{x:Type models:AgricultureReturns}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" />
                <TextBlock Text="{Binding FieldSizeinAcres}" />
                <!-- etc -->
            </StackPanel>
        </DataTemplate>

        <DataTemplate TargetType="{x:Type models:CorporateAsset}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding CorporateOfficeName}" />
                <TextBlock Text="{Binding NetWorth}" />
                <!-- etc -->
            </StackPanel>
        </DataTemplate>
    </ItemsControl.Resources>
</ItemsControl>

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

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