1.用在textbox等输入控件上,验证输入是否合法。

<Window.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<!--Binding属性会把数据源不断送过来,送过来的值与Value属性一致就被触发。-->
<DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self},Path=Text.Length}" Value="0">
<!--这里使用RelativeSource将控件自己作为数据源,当文本框文本长度为0时触发-->
<!--也可以用Converter来转换成Bool值验证长度、格式(Convert方法中用正则表达式验证)等
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Text,Converter={StaticResource dc}}" Value="True">
-->
<Setter Property="BorderBrush" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style >
</Window.Resources> <TextBox Height="23" HorizontalAlignment="Left" Margin="50,66,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="50,108,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />

运行结果:文本框文本长度为0,则显示设置的样式。

  DataTrigger的几个用法-LMLPHP

2.combox等列表控件里面,也可以用做数据筛选。

  对于集合数据或者列表数据,满足一定条件的就显示成另外一种样式。

1)先添加一个转化器:

class Text2BoolConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return false;
if (string.IsNullOrEmpty(value.ToString())) return false;
if (!Regex.IsMatch(value.ToString(), "^[1-9]\\d*$")) return false; return int.Parse(value.ToString()) < ;
} public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}

2)映射命名空间,把转换器放在资源中:

xmlns:c="clr-namespace:WpfDemo.VelueConvert"

 <Window.Resources>
<c:Text2BoolConvert x:Key="t2b"></c:Text2BoolConvert>
</Window.Resources>

3)添加ComBox显示列表 

   <ComboBox Height="23" HorizontalAlignment="Left" Margin="334,83,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120">
<!--ItemTemplate-->
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding Display}" Name="t1"></TextBlock>
<TextBlock Text="--" Name="t2"/>
<TextBlock Text="{Binding Tag}" Name="t3"></TextBlock>
</StackPanel>
<!--DataTrigger -->
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Tag,Converter={StaticResource t2b}}" Value="True">
<Setter Property="Foreground" TargetName="t1" Value="Red" />
<Setter Property="Foreground" TargetName="t2" Value="Red" />
<Setter Property="Foreground" TargetName="t3" Value="Red" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>

4)在Window加载的时候给ComBox指定数据源 

 List<object> objList = new List<object>();
for (int i = ; i < ; i += )
{
objList.Add(new { Display = "d" + i.ToString(), Tag = i.ToString() });
} comboBox1.ItemsSource = objList;

3.特殊状态的样式

<!--EditMode的背景-->
<UserControl.Resources>
<Style x:Key="menuText" TargetType="TextBlock" >
<Style.Triggers>
<!--设置修改时候的样式-->
<DataTrigger Binding="{Binding EditMode}" Value="0">
<Setter Property="Foreground" Value="DimGray" />
</DataTrigger>
</Style.Triggers>
<Setter Property="FontSize" Value="13" />
</Style>
</UserControl.Resources>
05-03 23:40