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.


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


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.


Figure 1.

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

XAML code

<Window x:Class="MainWindow"



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


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


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

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


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

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




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


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

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

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

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






        <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" />




Now run the application and test it.


Figure 2.

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


Figure 3.

Now move the mouse over the Button control.


Figure 4.

Now click on the TextBox. It shows IsFocused action.


Figure 5.


More Articles

© 2020 DotNetHeaven. All rights reserved.