Perform certain judgments based on data, which can be done using DataTrigger. The binding property of the DataTrigger object will send the data continuously, and once the value sent is the same as that of the Value property, DataTrigger is triggered. Examples are as follows:
XAML Code:
<window x:class= "Mytestwpfapplication.mainwindow"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns:local= "Clr-namespace:mytestwpfapplication"
Xmlns:i= "Http://schemas.microsoft.com/expression/2010/interactivity"
Xmlns:c= "Clr-namespace:system.collections;assembly=mscorlib"
Title= "WPF" height= "width=" windowstartuplocation= "Centerscreen" >
<Window.Resources>
<local:l2bconverter x:key= "CVTR"/>
<style targettype= "TextBox" >
<Style.Triggers>
<datatrigger binding= "{Binding relativesource={x:static relativesource.self}, Path=text.length, Converter={ StaticResource RESOURCEKEY=CVTR}} "value=" false ">
<setter property= "BorderBrush" value= "Red"/>
<setter property= "BorderThickness" value= "1"/>
<setter property= "ToolTip" value= "Please enter data greater than 6 characters"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<textbox text= "{Binding Test, Mode=oneway, updatesourcetrigger=propertychanged}" margin= "5"/>
<textbox margin= "5,0"/>
<textbox margin= "5"/>
</StackPanel>
</Window>
C # code:
{
public class L2bconverter:ivalueconverter
{
public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
int textLength = (int) value;
Return textLength > 6? True:false;
}
public object Convertback (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException ();
}
}
<summary>
The interactive logic of MainWindow.xaml
</summary>
public partial class Mainwindow:window
{
Public MainWindow ()
{
InitializeComponent ();
}
}
==========================================================================
Requirements that can be triggered when multiple data conditions are met may use Multidatatrigger. Examples are as follows:
XAML Code:
<window x:class= "Mytestwpfapplication.mainwindow"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns:local= "Clr-namespace:mytestwpfapplication"
Xmlns:i= "Http://schemas.microsoft.com/expression/2010/interactivity"
Xmlns:c= "Clr-namespace:system.collections;assembly=mscorlib"
Title= "WPF" height= "146" width= "windowstartuplocation=" >
<Window.Resources>
<style targettype= "ListBoxItem" >
<setter property= "ContentTemplate" >
<Setter.Value>
<DataTemplate>
<stackpanel orientation= "Horizontal" >
<textblock text= "{Binding ID}" width= "/>
<textblock text= "{Binding Name}" width= "/>
<textblock text= "{Binding age}" width= "/>"
</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>
=====================================================================
EventTrigger is triggered by an event and is triggered to perform an animation. The animation effects of the UI layer are often associated with EventTrigger. Although triggers are used extensively in a style, triggers are not only applied in the style----the various template can also have their own triggers. Examples are as follows:
<window x:class= "Mytestwpfapplication.mainwindow"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns:local= "Clr-namespace:mytestwpfapplication"
Xmlns:i= "Http://schemas.microsoft.com/expression/2010/interactivity"
Xmlns:c= "Clr-namespace:system.collections;assembly=mscorlib"
Title= "WPF" height= "width=" windowstartuplocation= "Centerscreen" >
<Window.Resources>
<style targettype= "button" >
<Style.Triggers>
<eventtrigger routedevent= "MouseEnter" >
<BeginStoryboard>
<Storyboard>
<doubleanimation to= "duration=" "0:0:0.2" storyboard.targetproperty= "Width"/>
<doubleanimation to= "duration=" "0:0:0.2" storyboard.targetproperty= "Height"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<eventtrigger routedevent= "MouseLeave" >
<BeginStoryboard>
<Storyboard>
<doubleanimation duration= "0:0:0.2" storyboard.targetproperty= "Width"/>
<doubleanimation duration= "0:0:0.2" storyboard.targetproperty= "Height"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Canvas>
<button width= "height=" "All" content= "OK"/>
</Canvas>
</Window>
This article is from the "Ink Pool small" blog, please be sure to keep this source http://306702895.blog.51cto.com/8366753/1637372
wpf--notes in layman's (2015.04.22)