3. 由數據觸發的DataTrigger 程序中京城會遇到基於數據執行某些判斷情況,遇到這種情況時我們可以考慮使用DataTriger。DataTriger對象的Binding屬性會把數據源源不斷送過來,一旦送過來的值與Value屬性一致,DataTriger就會觸發。 [csharp] using System; using System.Windows.Data; namespace WpfApp4.Converters { class StringToBooleanConverter:IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string str = value as string; if (!string.IsNullOrEmpty(str) && str.Length > 6) return false; else return true; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } using System; using System.Windows.Data; namespace WpfApp4.Converters { class StringToBooleanConverter:IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string str = value as string; if (!string.IsNullOrEmpty(str) && str.Length > 6) return false; else return true; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } } [html] <Window x:Class="WpfApp4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:converters="clr-namespace:WpfApp4.Converters" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <converters:StringToBooleanConverter x:Key="cvtr" /> <Style TargetType="{x:Type TextBox}"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={x:Static Member=RelativeSource.Self}, Path=Text, Converter={StaticResource cvtr}}" Value="false"> <Setter Property="BorderBrush" Value="Red"/> <Setter Property="BorderThickness" Value="1"/> </DataTrigger> </Style.Triggers> </Style> </Window.Resources> <StackPanel> <TextBox Margin="5"/> </StackPanel> </Window> <Window x:Class="WpfApp4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:converters="clr-namespace:WpfApp4.Converters" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <converters:StringToBooleanConverter x:Key="cvtr" /> <Style TargetType="{x:Type TextBox}"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={x:Static Member=RelativeSource.Self}, Path=Text, Converter={StaticResource cvtr}}" Value="false"> <Setter Property="BorderBrush" Value="Red"/> <Setter Property="BorderThickness" Value="1"/> </DataTrigger> </Style.Triggers> </Style> </Window.Resources> <StackPanel> <TextBox Margin="5"/> </StackPanel> </Window> 4. 多數據條件觸發的MultiDataTrigger 有時我們會遇到要求多個數據條件同時滿足時才能觸發變化的需求,此時可以考慮使用MultiDataTriger。 [html] <Window x:Class="WpfApp4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:converters="clr-namespace:WpfApp4.Converters" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <converters:StringToBooleanConverter x:Key="cvtr" /> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding ID}" Width="60"/> <TextBlock Text="{Binding Name}" Width="120"/> <TextBlock Text="{Binding Age}" Width="60"/> </StackPanel> </DataTemplate> </Setter.Value> </Setter> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding Path=ID}" Value="2"/> <Condition Binding="{Binding Path=Name}" Value="Tom"/> </MultiDataTrigger.Conditions> <MultiDataTrigger.Setters> <Setter Property="Background" Value="Orange"/> </MultiDataTrigger.Setters> </MultiDataTrigger> </Style.Triggers> </Style> </Window.Resources> <StackPanel> <ListBox x:Name="listBoxStudent" Margin="5"/> </StackPanel> </Window> <Window x:Class="WpfApp4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:converters="clr-namespace:WpfApp4.Converters" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <converters:StringToBooleanConverter x:Key="cvtr" /> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding ID}" Width="60"/> <TextBlock Text="{Binding Name}" Width="120"/> <TextBlock Text="{Binding Age}" Width="60"/> </StackPanel> </DataTemplate> </Setter.Value> </Setter> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding Path=ID}" Value="2"/> <Condition Binding="{Binding Path=Name}" Value="Tom"/> </MultiDataTrigger.Conditions> <MultiDataTrigger.Setters> <Setter Property="Background" Value="Orange"/> </MultiDataTrigger.Setters> </MultiDataTrigger> </Style.Triggers> </Style> </Window.Resources> <StackPanel> <ListBox x:Name="listBoxStudent" Margin="5"/> </StackPanel> </Window> [csharp] public partial class MainWindow : Window { List<Student> stus = new List<Student>() { new Student() { ID = 1, Name = "Anders", Age = 27 }, new Student() { ID = 2, Name = "Tom", Age = 22 }, new Student() { ID = 3, Name = "Jim", Age = 23 }, new Student() { ID = 4, Name = "Jack", Age = 24 }, new Student() { ID = 5, Name = "Mary", Age = 25 }, }; public MainWindow() { www.2cto.com InitializeComponent(); this.listBoxStudent.ItemsSource = this.stus; } } public class Student { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } }