ARTICLE

WPF Trigger in VB.NET

Posted by Rohatash Kumar Articles | XAML in VB.NET March 09, 2011
Here we will see that how to create trigger and also performs action to applies property value of the trigger.
 
Reader Level:

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

Creating one TextBox and one Button control on the window form.

XAML code

<TextBox Height="23" HorizontalAlignment="Left" Margin="69,62,0,0" Name="TextBox1" VerticalAlignment="Top" Width="120" />

<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="91,122,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />

</Grid>

The form looks like this.

t1.gif

Figure1.gif

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

Using Trigger for the TextBox

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

   <Style.Triggers>

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

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

          </Trigger>

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

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

               </Trigger>

                 </Style.Triggers>

                  </Style>

Using trigger for Button Control.

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

The above code defines that for the TextBox the Trigger property is set to false.

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

For the Button the Trigger property is set to true.

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

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="False">

                    <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="128,84,0,0" Name="TextBox1" VerticalAlignment="Top" Width="159" />

        <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 move the mouse over the TextBox and Button control.

t2.gif

Figure2.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor