WPF triggers (Trigger, DataTrigger, EventTrigger)

Source: Internet
Author: User
Tags visibility

There is something called a Trigger in WPF (remember not the trigger of the database). Its main function is to automatically change the appearance properties according to the different conditions of the trigger, or perform actions such as animations.

The main types of Wpftrigger are: Trigger, MultiTrigger, DataTrigger, Multidatatrigger, EventTrigger. In the literal sense we think you already know a general, and then I will also use the example code to introduce each. Trigger the main use of the scene in style, ControlTemplate, DataTemplate three places. Trigger can be used in these places, depending on the situation.

1. Use various trigger in style

The trigger used in style is primarily a trigger for a property, and a trigger is thrown when the value of the property changes.

A. Normal properties trigger when the mouse-over font turns red

	<checkbox content= "Style Trigger MouseOver Red" >
            <CheckBox.Resources>
                <style targettype= "{x: Type CheckBox} ">
                    <setter property=" Foreground "value=" Skyblue "/>
                    <Style.Triggers>
                        < !--Mouse-over font is red--
                        <trigger property= "IsMouseOver" value= "True" >
                            <setter property= "Foreground" Value= "Red"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </ Checkbox.resources>
        </CheckBox>

B. Normal attribute MultiTrigger when checkbox is checked and the mouse-over font turns green

	<checkbox content= "Style multitrigger Checked and MouseOver Green" >
            <CheckBox.Resources>
                < Style targettype= "{x:type CheckBox}" >
                    <setter property= "Foreground" value= "Skyblue"/>
                    < style.triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <condition property= " IsChecked "value=" true "/>
                                <condition property=" IsMouseOver "value=" true "/>
                            </ multitrigger.conditions>
                            <setter property= "Foreground" value= "Green"/>
                        </MultiTrigger>
                    </Style.Triggers>
                </Style>
            </CheckBox.Resources>
        </CheckBox>


C.eventtrigger mouse stroke length becomes shorter

 <checkbox content= "Style eventtrigger" width= "horizontalalignment=" "Left" > &LT;CHECKBOX.RESOURCES&G
                T
                    <style targettype= "{x:type CheckBox}" > <setter property= "Foreground" value= "Skyblue"/>
                            <Style.Triggers> <eventtrigger routedevent= "Mouse.mouseenter" >
                                    <EventTrigger.Actions> <BeginStoryboard>
                                          <Storyboard> <doubleanimation
                                          duration= "0:0:0.2" storyboard.targetproperty= "Width"
                                to= "/> </Storyboard>"
                        </BeginStoryboard> </EventTrigger.Actions> </eventtrigger> <eventtrigger routedevent= "Mouse.mouseleave" >
                                    <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <doubleanimation D
                                          uration= "0:0:0.2" storyboard.targetproperty= "Width" To= "/> </Storyboard> </" Beginstoryboard> </EventTrigger.Actions> </eventtrigger&gt
                    ; </Style.Triggers> </Style> </CheckBox.Resources> </CheckBox>

2. Using Trigger in ControlTemplate

The trigger used in ControlTemplate is primarily a trigger for an element in ControlTemplate, and a trigger is thrown when the value of the property changes.

The mouse with the property "Border" is sliding out of the background color, the other trigger the same style as above there are not many examples here.

<button content= "ControlTemplate" width= "height=" > <Button.Resources> <
                    Style targettype= "{x:type button}" > <setter property= "Background" value= "Gray"/> <setter property= "BorderBrush" value= "Black"/> <setter property= "Cursor" value= "Hand"
                            /> <setter property= "Template" > <Setter.Value>
                                    <controltemplate targettype= "{x:type button}" > <StackPanel>
                                    <border height= "Ten" background= "Red" ></Border> <border x:name= "Border" cornerradius= "0" borderthickness= "1" background= "{TemplateBinding Background}" Borderbrush= "{TemplateBinding BorderBrush}" > <contentpresenter verticalalign Ment= "Center" horizontalalignment= "CEnter "/> </Border> </StackPanel>
                                    <ControlTemplate.Triggers> <trigger property= "IsMouseOver" value= "True" > <setter targetname= "B  Order "property=" Background "value=" #FFC2E0FF "/> <setter targetname=" Border "
                                Property= "BorderBrush" value= "#FF3399FF"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> & lt;/setter.value> </Setter> </Style> </button.resources&
        Gt </Button>

3. Using Trigger in DataTemplate

Using Trigger in DataTemplate can display different content depending on the data being bound.

<treeview name= "_tree" margin= "0" borderthickness= "0" verticalalignment= "Stretch" background= "Transparent" Itemssource= "{Binding children}" > <TreeView.ItemTemplate>  

This is a TreeView control depending on the isdir value of the binding can show that icon is a directory (folder) or a file, see is not very simple

The above is simply the introduction of some of the use of trigger, the specific functions of everyone can be free to play, which is the strength of WPF



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.