WPF Use Trigger in VB.NET

In this article we will learn How to use trigger in WPF using VB.NET.
  • 2455
 

In this article we will learn How to use Trigger in WPF using VB.NET.

Trigger

A trigger is used to performs actions and to perform action applies property value of the trigger.

Properties

Setter - This property describes the values to apply when the specified condition has been met.

Value -  This property represents the value to be compared with the property value of the control (element).

SourceName - This property represents the name of the object with the property that causes the associated setters to be applied.

Property - This property returns the value that is used to compared with the Value property of the trigger.

For example

Drag and Drop two TextBox and one Button control on the form. The form looks like this.

t1.gif

Figure 1.

Now using IsMousedOver and IsFocused action on the TextBox and Button control.

XAML code

<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">

    <Window.Resources>

      <Style TargetType="{x:Type TextBox }">

            <Style.Triggers>

                <Trigger Property="IsMouseOver" Value="True">

                    <Setter Property="Background" Value="red" />

                </Trigger>

                <Trigger Property="IsFocused" Value="True">

                    <Setter Property="Background" Value="Red" />

                </Trigger>

               </Style.Triggers>

        </Style>

        <Style TargetType="{x:Type Button}">

            <Style.Triggers>

                <Trigger Property="IsMouseOver" Value=" True">

            <Setter Property="FontWeight" Value="Bold"/>

            <Setter Property="Foreground" Value="Red"/>

              <Setter Property="Background" Value=" Green"/>

            </Trigger>

                </Style.Triggers>

        </Style>

    </Window.Resources>

    <Grid>

        <TextBox Height="64" HorizontalAlignment="Left" Margin="24,83,0,0" Name="TextBox1" VerticalAlignment="Top" Width="159" />

        <TextBox Height="64" HorizontalAlignment="Right" Margin="0,87,113,0" Name="TextBox2" VerticalAlignment="Top" Width="187" />

        <Button Content="Button" Height="33" HorizontalAlignment="Left" Margin="107,184,0,0" Name="Button1" VerticalAlignment="Top" Width="218" />

       

    </Grid>

</Window>

Now run the application and test it.

t2.gif

Figure 2.

Now move the mouse over the TextBox control. It shows IsMousedOver action.

t3.gif

Figure 3.

Now move the mouse over the Button control.

t4.gif

Figure 4.

Now click on the TextBox. It shows IsFocused action.

t5.gif

Figure 5.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.