• WPF 示例自定义的 DataTemplateSelector


    由于实际环境无法在这里直接运行代码,但我可以提供一个WPF中使用DataTemplateSelector根据数据类型动态选择显示控件的示例代码。请将此代码片段复制到您的WPF项目中,并确保已定义了对应的ViewModel和数据类型。
    首先,定义两个简单的数据类:

    public class MyDataType1
    {
        public string Property1 { get; set; }
    }
    
    public class MyDataType2
    {
        public int Property2 { get; set; }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    然后创建自定义的
    DataTemplateSelector:

    using System.Windows;
    using System.Windows.Controls;
    
    public class MyTemplateSelector : DataTemplateSelector
    {
        public DataTemplate Template1 { get; set; }
        public DataTemplate Template2 { get; set; }
    
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item is MyDataType1)
                return Template1;
            else if (item is MyDataType2)
                return Template2;
            else
                return base.SelectTemplate(item, container);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在XAML中注册并使用这些模板:

    <Window x:Class="YourNamespace.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:YourNamespace"> 
    
        <Window.Resources>
            <DataTemplate x:Key="Type1Template">
                <TextBlock Text="{Binding Property1}" Foreground="Red" />
            DataTemplate>
            
            <DataTemplate x:Key="Type2Template">
                <TextBlock Text="{Binding Property2}" Foreground="Blue" />
            DataTemplate>
    
            <local:MyTemplateSelector x:Key="MySelector">
                <local:MyTemplateSelector.Template1>
                    <StaticResource ResourceKey="Type1Template" />
                local:MyTemplateSelector.Template1>
                <local:MyTemplateSelector.Template2>
                    <StaticResource ResourceKey="Type2Template" />
                local:MyTemplateSelector.Template2>
            local:MyTemplateSelector>
        Window.Resources>
    
        <StackPanel>
            <ContentControl Content="{Binding YourDataType1Instance}" ContentTemplateSelector="{StaticResource MySelector}"/>
            <ContentControl Content="{Binding YourDataType2Instance}" ContentTemplateSelector="{StaticResource MySelector}"/>
        StackPanel>
    Window>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    最后,在对应的ViewModel或代码后置中设置数据上下文(DataContext),包含
    YourDataType1Instance

    YourDataType2Instance
    实例。这样,当窗口加载时,ContentControl将会根据内容的数据类型自动应用相应的模板样式。

    第二种方式:

    当然,我可以为您提供一个简单的 WPF 示例程序,其中包含一个自定义的 DataTemplateSelector。以下是一个基本示例代码

    MainWindow.xaml:

    <Window x:Class="DataTemplateSelectorExample.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:DataTemplateSelectorExample"
            Title="DataTemplateSelector Example" Height="350" Width="525">
    
        <Window.Resources>
            <DataTemplate x:Key="Template1">
                <TextBlock Text="Template 1" />
            DataTemplate>
    
            <DataTemplate x:Key="Template2">
                <TextBlock Text="Template 2" />
            DataTemplate>
    
            <local:CustomDataTemplateSelector x:Key="TemplateSelector" />
        Window.Resources>
    
        <Grid>
            <ContentControl Content="{binding MyProperty}" ContentTemplateSelector="{StaticResource TemplateSelector}"/>
        Grid>
    Window>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    DataItem.cs:

    using System.Windows;
    
    namespace DataTemplateSelectorExample
    {
        public class DataItem : FrameworkElement
        {
            public string TemplateType
            {
                get { return (string)GetValue(TemplateTypeProperty); }
                set { SetValue(TemplateTypeProperty, value); }
            }
    
            public static readonly DependencyProperty TemplateTypeProperty =
                DependencyProperty.Register("TemplateType", typeof(string), typeof(DataItem), new PropertyMetadata(null));
        }
    	
    	public class DataItemA: DataItem
    	{
    		TemplateType = Template1;
    	}
    	
    	public class DataItemB: DataItem
    	{
    		TemplateType = Template2;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    CustomDataTemplateSelector.cs:

    using System.Windows;
    using System.Windows.Controls;
    
    namespace DataTemplateSelectorExample
    {
        public class CustomDataTemplateSelector : DataTemplateSelector
        {
            public override DataTemplate SelectTemplate(object item, DependencyObject container)
            {
                if (item is string name)
                {
                    switch (name)
                    {
                        case "Template1":
                            return (DataTemplate)((FrameworkElement)container).FindResource("Template1");
                        case "Template2":
                            return (DataTemplate)((FrameworkElement)container).FindResource("Template2");
                    }
                }
                return base.SelectTemplate(item, container);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这个示例程序中,我们定义了两种不同的 DataTemplate (Template1 和 Template2),然后创建了一个 CustomDataTemplateSelector 类来根据 DataItem 的 TemplateType 属性选择相应的 DataTemplate。最后在 MainWindow.xaml 中使用 ContentControl 来展示 DataItem,并利用 TemplateSelector 来选择应用的 DataTemplate。

    请注意,这只是一个简单的示例。在实际应用中,您可能需要根据实际需求来扩展和定制 DataTemplateSelector。希望这个示例能帮助您理解如何在 WPF 中使用 DataTemplateSelector。

    一个数据模板示例:

    
    <Window x:Class="WpfApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApp"
            Title="MainWindow" Height="450" Width="800">
        <Window.Resources>
            <DataTemplate x:Key="TextTemplate">
                <TextBlock Text="{Binding Text}" />
            DataTemplate>
    
            <DataTemplate x:Key="ImageTemplate">
                <Image Source="{Binding ImageSource}" Width="50" Height="50"/>
            DataTemplate>
    
            <DataTemplate x:Key="DefaultTemplate">
                <TextBlock Text="No template available for this object type" />
            DataTemplate>
        Window.Resources>
    
        <Grid>
            <ItemsControl ItemsSource="{Binding ModuleCollection}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <ContentPresenter Content="{Binding}">
                            <ContentPresenter.ContentTemplate>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}">
                                        <ContentControl.Style>
                                            <Style TargetType="ContentControl">
                                                "ContentTemplate" Value="{StaticResource DefaultTemplate}" />
                                                
                                                    "{Binding Type}" Value="Text">
                                                        "ContentTemplate" Value="{StaticResource TextTemplate}" />
                                                    
                                                    "{Binding Type}" Value="Image">
                                                        "ContentTemplate" Value="{StaticResource ImageTemplate}" />
                                                    
                                                
                                            Style>
                                        ContentControl.Style>
                                    ContentControl>
                                DataTemplate>
                            ContentPresenter.ContentTemplate>
                        ContentPresenter>
                    DataTemplate>
                ItemsControl.ItemTemplate>
            ItemsControl>
        Grid>
    Window>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
  • 相关阅读:
    简单介绍一下tensorflow与pytorch的相互转换(主要是tensorflow转pytorch)
    C语言每日一题(19)回文素数
    java-net-php-python-15体育用品网上销售系统计算机毕业设计程序
    【差旅游记】启程-新疆哈密(2)
    原语科技宣布完成千万级天使+轮融资,致力于打造隐私计算标准化产品
    PostgreSQL之SQL高级特性
    [附源码]计算机毕业设计JAVA大学生互助系统
    Vue-MVVM数据双向绑定响应式原理之Object.defineProperty
    单元测试实施最佳方案(背景、实施、覆盖率统计)
    在架构组工作是种什么体验?今天大鸡腿带你体验下~
  • 原文地址:https://blog.csdn.net/weixin_43542114/article/details/136269483