[Transfer] WPF template selector DataTemplateSelector and dynamic binding, the use of DataTemplate.Triggers triggers…

In general, you should create a DataTemplateSelector if there are multiple DataTemplates available for the same type of object and you want to provide your own logic for selecting which DataTemplate to apply based on the properties of each data object. Note that you can set the DataType property on the DataTemplate if you have objects of different types. If you do this, you don’t need to create a DataTemplateSelector. Also, consider using a DataTrigger or Data Converter if the object type is the same but the properties are different.
In layman’s terms, it means choosing different templates based on different data. Next, I use an example to describe the use of DataTemplateSelector and dynamic binding.

The example below is as shown in the figure. As long as the age is greater than 50, one template is used, otherwise another template is used, and the background of the template for men over 50 is added as a trigger and the background turns blue.

1. Create a template selector

public class MyDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var fe = container as FrameworkElement;
            var obj = item as Person;
            DataTemplate dt = null;
            if (obj != null & amp; & amp; fe != null)
            {
                if (obj. age > 50)
                    dt = fe.FindResource("one") as DataTemplate;
                else
                    dt = fe.FindResource("two") as DataTemplate;

            }
            return dt;
        }
    }

2. Interface design:

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication5"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:MyDataTemplateSelector x:Key="mydt"></local:MyDataTemplateSelector>
        <DataTemplate x:Key="one">
            <Border BorderThickness="2" BorderBrush="red" Background="AliceBlue">
                <StackPanel Orientation="Horizontal" Name="skp" >
                    <TextBlock Text="{Binding name}" Margin="10"></TextBlock>
                    <TextBlock Text="{Binding age}" Margin="10"></TextBlock>
                    <TextBlock Text="{Binding sex}" Margin="10"></TextBlock>
                </StackPanel>
            </Border>
            <DataTemplate.Triggers>
                <DataTrigger Value="Male" Binding="{Binding Path=sex}">
                    <Setter TargetName="skp" Property="Background" Value="CornflowerBlue" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <DataTemplate x:Key="two" >
            <Border BorderThickness="1" BorderBrush="Blue" Background="YellowGreen" Padding="5" >
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding name}" Margin="10"></TextBlock>
                    <TextBlock Text="{Binding age}" Margin="10"></TextBlock>
                    <TextBlock Text="{Binding sex}" Margin="10"></TextBlock>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListBox Name="lb" ItemTemplateSelector="{StaticResource mydt}">
            
        </ListBox>
    </Grid>
</Window>

3. Backstage:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadData();
            lb.ItemsSource = list;
        }
        public List<Person> list { get; set; }
        public void LoadData()
        {
            Random r = new Random();
            list = new List<Person>();
            for (int i = 0; i < 10; i + + )
            {
                list.Add(new Person
                {
                    name = "Zhang San" + i,
                    age = r.Next(100)
                });
            }
        }
    }
    public class Person
    {
        public string name { get; set; }
        public int age { get; set; }
        
    }

Original address: https://www.cnblogs.com/lunawzh/p/5983069.html