我可以在XAML(.NET 4 Framework之前版本)中指定泛型类型吗?

Mat*_*ton 72 c# generics wpf xaml

在XAML中,我可以声明一个DataTemplate,以便在显示特定类型时使用该模板.例如,此DataTemplate将使用TextBlock显示客户的名称:

<DataTemplate DataType="{x:Type my:Customer}">
    <TextBlock Text="{Binding Name}" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

我想知道是否可以定义一个将在IList <Customer>显示时使用的DataTemplate.因此,如果ContentControl的内容是ObservableCollection <Customer>,它将使用该模板.

是否可以使用{x:Type}标记扩展在XAML中声明类似IList的泛型类型?

Ian*_*kes 31

不是直接在XAML中,但您可以DataTemplateSelector从XAML 引用选择正确的模板.

public class CustomerTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item,
                                                DependencyObject container)
    {
        DataTemplate template = null;
        if (item != null)
        {
            FrameworkElement element = container as FrameworkElement;
            if (element != null)
            {
                string templateName = item is ObservableCollection<MyCustomer> ?
                    "MyCustomerTemplate" : "YourCustomerTemplate";

                template = element.FindResource(templateName) as DataTemplate;
            } 
        }
        return template;
    }
}

public class MyCustomer
{
    public string CustomerName { get; set; }
}

public class YourCustomer
{
    public string CustomerName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

资源字典:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    >
    <DataTemplate x:Key="MyCustomerTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="150"/>
            </Grid.RowDefinitions>
            <TextBlock Text="My Customer Template"/>
            <ListBox ItemsSource="{Binding}"
                     DisplayMemberPath="CustomerName"
                     Grid.Row="1"/>
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="YourCustomerTemplate">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="150"/>
            </Grid.RowDefinitions>
            <TextBlock Text="Your Customer Template"/>
            <ListBox ItemsSource="{Binding}"
                     DisplayMemberPath="CustomerName"
                     Grid.Row="1"/>
        </Grid>
    </DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

窗口XAML:

<Window 
    x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" 
    Height="300" 
    Width="300"
    xmlns:local="clr-namespace:WpfApplication1"
    >
    <Grid>
        <Grid.Resources>
            <local:CustomerTemplateSelector x:Key="templateSelector"/>
        </Grid.Resources>
        <ContentControl 
            Content="{Binding}" 
            ContentTemplateSelector="{StaticResource templateSelector}" 
            />
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

后面的窗口代码:

public partial class Window1
{
    public Window1()
    {
        InitializeComponent();
        ObservableCollection<MyCustomer> myCustomers
            = new ObservableCollection<MyCustomer>()
        {
            new MyCustomer(){CustomerName="Paul"},
            new MyCustomer(){CustomerName="John"},
            new MyCustomer(){CustomerName="Mary"}
        };

        ObservableCollection<YourCustomer> yourCustomers
            = new ObservableCollection<YourCustomer>()
        {
            new YourCustomer(){CustomerName="Peter"},
            new YourCustomer(){CustomerName="Chris"},
            new YourCustomer(){CustomerName="Jan"}
        };
        //DataContext = myCustomers;
        DataContext = yourCustomers;
    }
}
Run Code Online (Sandbox Code Playgroud)


age*_*ped 24

没有开箱即用,没有; 但是那些有进取心的开发人员已经这样做了.

例如,微软的迈克希尔伯格在这篇文章中用它来玩.Google当然还有其他人.


Cla*_*ila 21

您还可以将通用类包装在指定T的派生类中

public class StringList : List<String>{}
Run Code Online (Sandbox Code Playgroud)

并使用XAML中的StringList.


cpl*_*tts 7

aelij(WPF Contrib项目的项目协调员)有另一种方法可以做到这一点.

什么甚至更酷(即使将来某个时候关闭)...是XAML 2009(XAML 2006是当前版本)本来会支持这个.查看此PDC 2008会话以获取有关它的信息等.

  • 在松散的xaml文件中仅支持XAML 2009(从.NET 4.0开始,WPF 4.0).也就是说,Blend,Cider(Visual Studio设计器)和编译的BAML(这是嵌入式xaml编译成的)...不支持新语法.希望这将在未来版本的WPF中发生变化.请参阅以下链接并投票:http://dotnet.uservoice.com/forums/40583-wpf-feature-suggestions/suggestions/478860-support-xaml2009-throughout#ref=title (5认同)