[英]binding a generic property of a model in wpf
我有一个wpf用户控件,它已绑定到模型(CustomerTaxModel),并且它具有一个listview和GridView,应在下面显示“ T”资产类型的属性数据。 类型T是仅具有属性的类,它具有可变数量的属性,具体取决于我们从第三方税务验证公司收到的数据。
public class CustomerTaxModel
{
public int ID { get; set; }
public string Name { get; set; }
public T Assets { get; set; }
}
我的通用类型“ T”类如下所示。 它们可能没有匹配的属性。 我应该在第一列中显示T类型的数据,并带有属性名称,在第二列中显示它们的值。 可以在xaml中进行这种类型的绑定吗? 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; }
}
我尝试的一种解决方案是绑定到KeyValuePair变量,而不是直接绑定有效的“ T”类型。 我引用了链接,并尝试使用静态属性类代替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
通过反射起作用。 它可以查找实际运行时对象具有的任何公共实例属性。 这就是WPF控件的DataContext
属性可以是Object
类型的方式。 就WPF而言,您的Assets
属性可以是对象,但没有理由放弃模型中的强类型。 WPF吃任何东西。
这给了我一个TextBlock
,上面写着“ Fred”:
public MainWindow()
{
InitializeComponent();
DataContext = new CustomerTaxModel<AgricultureReturns>()
{
Assets = new AgricultureReturns() { Name = "Fred" }
};
}
XAML:
<TextBlock Text="{Binding Assets.Name}" />
如果您有一个CustomerTaxModel
的ObservableCollection
,则常见的模式是为Assets
可能具有的不同类型提供一堆DataTemplates
。 这些是非常糟糕的模板,但是您可以理解。
<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.