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" />
补充:软件开发 , C# ,