代码之家  ›  专栏  ›  技术社区  ›  Matt Hamilton

我可以在XAML(在.NET4框架之前)中指定泛型类型吗?

  •  72
  • Matt Hamilton  · 技术社区  · 18 年前

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

    <DataTemplate DataType="{x:Type my:Customer}">
        <TextBlock Text="{Binding Name}" />
    </DataTemplate>
    

    6 回复  |  直到 10 年前
        1
  •  32
  •   Samuel Slade    14 年前

    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; }
    }
    

    资源字典:

    <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>
    

    <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>
    

    窗口代码隐藏:

    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;
        }
    }
    
        2
  •  24
  •   ageektrapped    18 年前

    不是现成的,不是;但也有一些有进取心的开发者已经这样做了。

    微软公司的迈克·希尔伯格(Mike Hillberg)在2010年就利用了它 this post 例如谷歌当然还有其他的。

        3
  •  21
  •   Claudiu Mihaila    15 年前

    public class StringList : List<String>{}
    

        4
  •  7
  •   cplotts    17 年前

    aelij(项目的项目协调员) WPF Contrib 项目)有另一个 way 去做吧。

    更酷的是什么(即使它在未来的某个时候是关闭的)。。。XAML2009(XAML2006是当前版本)将在本机上支持此功能。看看这个 PDC 2008 session 了解更多信息。

        5
  •  0
  •   Mike de Klerk    9 年前

    public class MyType : List<int> { }
    

    并在xaml中使用它,例如

    <DataTemplate DataType={x:Type myNamespace:MyType}>