使用数据模版选择器及数据触发器
通过定义数据模板触发器,当满足某种条件时执行。如当鼠标悬停时,改变数据的背景色、前景色。
通过定义数据模版选择器,可以根据条件将不同的模板应用于不同的数据。如本例中,年龄小于20的学生和大于20的学生使用不同的数据模板。
先看效果:
鼠标悬停时的效果:
代码如下:
<Window x:Class="DataTemplateDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DataTemplateDemo"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:MyTemplate x:Key="template"></local:MyTemplate>
<DataTemplate x:Key="ListBoxItem">
<Border x:Name="brd" BorderBrush="Green" BorderThickness="2" Width="200">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=First}"></Label>
<Label Content="{Binding Path=Last}"></Label>
<Label Content="{Binding Path=Age}"></Label>
</StackPanel>
</Border>
<DataTemplate.Triggers>
<Trigger SourceName="brd" Property="IsMouseOver" Value="True">
<Setter TargetName="brd" Property="Background" Value="Red"></Setter>
<Setter TargetName="brd" Property="BorderBrush" Value="Green"></Setter>
<Setter TargetName="brd" Property="BorderThickness" Value="5"></Setter>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
<DataTemplate x:Key="OtherListBoxItem">
<Border x:Name="brd" BorderBrush="Yellow" BorderThickness="2" Width="200">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=First}"></Label>
<Label Content="{Binding Path=Last}"></Label>
<Label Content="{Binding Path=Age}"></Label>
</StackPanel>
</Border>
<DataTemplate.Triggers>
<Trigger SourceName="brd" Property="IsMouseOver" Value="True">
<Setter TargetName="brd" Property="Background" Value="Red"></Setter>
<Setter TargetName="brd" Property="BorderBrush" Value="Green"></Setter>
<Setter TargetName="brd" Property="BorderThickness" Value="5"></Setter>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemTemplateSelector="{StaticResource template}" x:Name="lstStudent" Margin="73,47,111,72" BorderThickness="0">
<local:Student First="aa" Last="test" Age="18"></local:Student>
<local:Student First="bb" Last="test" Age="22"></local:Student>
<local:Student First="cc" Last="test" Age="19"></local:Student>
<local:Student First="dd" Last="test" Age="21"></local:Student>
</ListBox>
</Grid>
</Window>
MyTemplate类代码如下:
public class MyTemplate:DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
DataTemplate myDataTemplate;
FrameworkElement element = container as FrameworkElement;
Student stuItem = item as Student;
if (stuItem.Age < 20)
{
myDataTemplate = element.FindResource("ListBoxItem") as DataTemplate;
}
else
{
myDataTemplate = element.FindResource("OtherListBoxItem") as DataTemplate;
}
return myDataTemplate;
}
}
Student类代码如下:
namespace DataTemplateDemo
{
class Student
{
string first;
public string First
{
get { return first; }
set { first = value; }
}
string last;
public string Last
{
get { return last; }
set { last = value; }
}
int age;
public int Age
{
get { return age; }
set { age = value; }
}
}
}