【转】wpf 模板选择器DataTemplateSelector及动态绑定,DataTemplate.Triggers触发器的使用

时间:2021-09-04 11:58:26

通常,如果有多个 DataTemplate 可用于同一类型的对象,并且您希望根据每个数据对象的属性提供自己的逻辑来选择要应用的 DataTemplate,则应创建 DataTemplateSelector。请注意,如果具有不同类型的对象,则可以对 DataTemplate 设置 DataType 属性。如果您执行了此操作,则无需创建 DataTemplateSelector。此外,如果对象类型相同但属性不同,也可以考虑使用 DataTrigger 或数据转换器。
通俗讲,就是根据不同的数据选择不同的模板。接下来,我用一个例子来讲述DataTemplateSelector和动态绑定的使用方法。

下面例子如图,只要年龄大于50的使用一种模板,否则另一种模板,并在年龄大于50的模板中是男性的添加触发器使用背景变成蓝色的。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAApAAAAG2CAIAAABHyiGxAAAgAElEQVR4nO3dfXRV9Z3v8b1W76U3LZ4oYijaqEEZowwwIXaJziwXsyJQe0nIRLwl0BDbZJbVWWZFHc0yzGAHDA9KivUaUbQJ4UEEQYvlQRqHZHygVKGtjQ0yiMaVVigQcmKCULjK/eM87bP3Pidnn/P9nb3P5v1anz+ak5igs+5689t751ytAwAAuJ7W0dFxDgAA7/p1aWkGzfJfgWADALzP8QYTbAAAhuZ4gwk2AABDc7zBBBsAgKE53mCCDQDA0BxvMMEGAGBojjeYYAMAPCvfjvjfyvEGE2wAgGe5LdjnQxJ8nWADAC4Ibgu2ZZvFa51SsH/3+/c3vbJtyK14ZnVgm17Z9q8Llr+3//d//etfk/y/EnBhOLSqLD8/v2zVoUz6WW11afsz4wI3ZIYT/0rBmuoLraLWKQV7d8ebR4+ftLvOrv8+depUgv+tAXdrqwv8Hb6uLc4XBYo4xBdZ/SOJxy/w57D4+sAnzD9Z9zrBRsZxZ7D1nVZR61SD/dVXX8beV19++f8C07/+hz8eHBwcTPC/NeBugUaVlcXrVFtdfllZmb1gJ/cHMf+EWH+hiNXx1P8MBBtp4NpgqztbywT7hZf+6z+e27Vw1esLX3j9sZ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pCTrABwDleC3bw7cwi97An1+95M5VfpI46Uu9ZlKvgjVN6Oh65xnhJPPRh1JG6f9fDY1U/NB48Z4+Z/J1RmqYl8yYqBBsAVEgi1XquCrbuEbPQY2XBDw9vmpmdWrOTfs48wenfy8zwKPixdx641vw8mvhCB+vo9yV9t3GSpml5D7z5KcEGAEf5/f7BwcEB+wYHB90S7EhQdZfBjXk+vGlmtjZz/R+i33g8ngvj7cTDb5wS73b1u42TEnzXFIINAIp46pI4c8MINgCoQLCZ8Ag2AKhAsJnwCDYAqECwmfAINgCoQLCZ8Ag2AKhgDvb+/fvNeT5w4ADBZgmNYAOACuZgt7S0/Pa3v9W/0tPTs3btWoLNEhrBBgAVzMHevHlza2trV1dX4MPjx49v3rx5w4YNBJslNIINACqYg3306NGNGzeuWbPm8OHDfX1927dvb21tPXToEMFmCY1gA4AK5mAHroGvW7du7dq1r7/+ektLi+VdbYLNrEewAUAFy2D7/f7Dhw+vXbu2paWlvb3d/FmCzWKOYAOACrGC7ff7Dx48uH379r6+PoLNbIxgA4AKcYIdH8Fm1iPYAKACwWbCI9gAoIKrg32upvZ0WzvLrJ2rqSXYACDO1cFmmT0AgByCzZQNACDH7/cPDg4O2Dc4OKgy2AAAQCeJs7VeqsHu6/OfBQAAivX1+VMKNmOMMcbStiSDDQAA0sx2sAEAgEsQbAAAMgDBBgAgAxBsAAAyAMEGACADEGwAADIAwQYAIAMEgw0AAFzu/wPxmBxv6ZBW6wAAAABJRU5ErkJggg==" alt="" />

1、创建模板选择器

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 && fe != null)
{
if (obj.age > )
dt = fe.FindResource("one") as DataTemplate;
else
dt = fe.FindResource("two") as DataTemplate; }
return dt;
}
}

2、界面设计:

<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="" Width="">
<Window.Resources>
<local:MyDataTemplateSelector x:Key="mydt"></local:MyDataTemplateSelector>
<DataTemplate x:Key="one">
<Border BorderThickness="" BorderBrush="red" Background="AliceBlue">
<StackPanel Orientation="Horizontal" Name="skp" >
<TextBlock Text="{Binding name}" Margin=""></TextBlock>
<TextBlock Text="{Binding age}" Margin=""></TextBlock>
<TextBlock Text="{Binding sex}" Margin=""></TextBlock>
</StackPanel>
</Border>
<DataTemplate.Triggers>
<DataTrigger Value="男" Binding="{Binding Path=sex}">
<Setter TargetName="skp" Property="Background" Value="CornflowerBlue" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<DataTemplate x:Key="two" >
<Border BorderThickness="" BorderBrush="Blue" Background="YellowGreen" Padding="" >
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding name}" Margin=""></TextBlock>
<TextBlock Text="{Binding age}" Margin=""></TextBlock>
<TextBlock Text="{Binding sex}" Margin=""></TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox Name="lb" ItemTemplateSelector="{StaticResource mydt}"> </ListBox>
</Grid>
</Window>

3、后台:

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 = ; i < ; i++)
{
list.Add(new Person
{
name = "张三" + i,
age = r.Next()
});
}
}
}
public class Person
{
public string name { get; set; }
public int age { get; set; } }

原文地址:https://www.cnblogs.com/lunawzh/p/5983069.html