Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在wpf中绑定模型的泛型属性_C#_Wpf_Xaml_Generics_Mvvm - Fatal编程技术网

C# 在wpf中绑定模型的泛型属性

C# 在wpf中绑定模型的泛型属性,c#,wpf,xaml,generics,mvvm,C#,Wpf,Xaml,Generics,Mvvm,我有一个wpf usercontrol,它绑定到一个模型(CustomerTaxModel),它有一个listview和GridView,应该在下面显示类型为“T”资产的属性数据。此类型T是一个仅具有属性的类,它具有可变的属性数量,这取决于我们从第三方税务验证公司收到的数据 public class CustomerTaxModel { public int ID { get; set; } public string Name { get; set; } pub

我有一个wpf usercontrol,它绑定到一个模型(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型可用于企业茶具、农业轮作等

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}}"/>    


绑定。路径通过反射工作。它可以查找实际运行时对象具有的任何公共实例属性。这就是WPF控件的类型可以是
Object
的方式。就WPF而言,
资产
属性可以是对象,但没有理由放弃在模型中使用强类型。WPF什么都吃

这给了我一个写着“Fred”的
TextBlock


但是您上面指定的xaml绑定仍然需要我们显式地指定T类属性名(例如“{binding Assets.Name}”)?仅仅通过绑定datacontext,它就不能遍历属性列表并动态显示它们吗?@krrishna请参阅更新。没有标准的方法让WPF自动计算出您拥有的属性并显示它们。您可以为CustomerTaxModel提供一个“带值的命名属性”的集合,其中包含int、string等的不同子类型,并显示一个ItemsControl,其中包含上述隐式应用的模板。除非你有大量潜在的资产类型,否则我不会去那里。
public MainWindow()
{
    InitializeComponent();

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

}
<TextBlock Text="{Binding Assets.Name}" />
<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>