Monday, 2 April 2012

EventTrigger In Wpf:-

If you want to perform animations on mouse events we can use EventTriggers.Using EventTriggers we can start,pause and stop the storyboards.The following example  performs simple animations on mouse enter and mouse leave events.

<Window x:Class="WpfEventTriggers.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">
    <Grid>
        <Rectangle Name="rMain" Width="100" Height="100">
            <Rectangle.Fill>
                <SolidColorBrush x:Name="MySolidColorBrush" Color="Green" />
            </Rectangle.Fill>
            <Rectangle.Triggers>
                <!--Begins the storyboard on mouseenter-->
                <EventTrigger RoutedEvent="MouseEnter">
                    <BeginStoryboard  Name="sbMain">
                        <!--StoryBoard-->
                        <Storyboard>
                            <DoubleAnimation
                                            Storyboard.TargetName="rMain"
                                            Storyboard.TargetProperty="Width"
                                            From="100" To="200" Duration="0:0:1" >
                            </DoubleAnimation>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>

                <!--Stops the storyboard on mouseleave-->
                <EventTrigger RoutedEvent="MouseLeave">
                    <StopStoryboard>
                        <StopStoryboard.BeginStoryboardName>
                            <!--Specify storyboard name-->
                            sbMain
                        </StopStoryboard.BeginStoryboardName>
                    </StopStoryboard>
                </EventTrigger>
            </Rectangle.Triggers>
        </Rectangle>
    </Grid>
</Window>

Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com

MultiDataTrigger In Wpf:-

If you want to change the properties of a control based on more than  one property we have to use MultiDataTrigger.The following example display the text based on user role and designation.

->Create a user class with properties UserName,Address,Role and Designation.

 public class User
 {
        public string UserName { get; set; }
        public string Address { get; set; }
        public string Role { get; set; }
        public string Designation { get; set; }
 }

->Add the MultiDataTrigger.

<Window x:Class="WpfMultiDataTriggers.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" Loaded="Window_Loaded">
    <Grid>
        <Grid.Resources>
            <!--Add the style for label-->
            <Style TargetType="{x:Type Label}" x:Key="lbStyle">
                <Style.Triggers>
                    <!--Check the user role and designation-->
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=Role}" Value="Admin"></Condition>
                            <Condition Binding="{Binding Path=Designation}" Value="Professor"></Condition>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Content" Value="You have administration rights."></Setter>
                        <Setter Property="Background" Value="Green"></Setter>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <ListBox Name="lstUsers">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="200"></ColumnDefinition>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <!--Bind the user name-->
                        <Label Grid.Column="0" Content="{Binding UserName}"></Label>

                        <!--Bind the address-->
                        <Label Grid.Column="1" Content="{Binding Address}"></Label>

                        <!--Apply the style resource for label-->
                        <Label Grid.Column="2" Style="{StaticResource lbStyle}"></Label>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

->Bind the listbox in the window load event.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
      //User list
      List<User> lstUser = new List<User>();

      //Adding users.
      lstUser.Add(new User() { UserName = "David", Address = "Mumbai", Role = "Admin", Designation = "Professor" });

      lstUser.Add(new User() { UserName = "Anderson", Address = "Pune", Role = "Guest", Designation = "Clerk" });

       lstUser.Add(new User() { UserName = "Andri", Address = "Mumbai", Role = "Admin", Designation = "Clerk" });

       //Set the item source here.
       lstUsers.ItemsSource = lstUser;
}

Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com

Saturday, 31 March 2012

DataTrigger In Wpf:-

While binding we can change the properties of a control based  on the binding property value.

->Create a user class with properties UserName,Address and Role.


public class User
    {
        public string UserName { get; set; }

        public string Address { get; set; }

        public string Role { get; set; }
    }

->Add the DataTrigger.

<Window x:Class="WpfDataTriggers.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" Loaded="Window_Loaded">
    <Grid>
        <Grid.Resources>
            <!--Add the style for label-->
            <Style TargetType="{x:Type Label}" x:Key="lbStyle">
                <Style.Triggers>
                    <!--Check the user  rights-->
                    <!--If the user is admin-->
                    <DataTrigger Binding="{Binding Role}" Value="Admin">
                        <Setter Property="Content" Value="You have administration rights."></Setter>
                        <Setter Property="Background" Value="Green"></Setter>
                    </DataTrigger>
                    <!--If the user is guest-->
                    <DataTrigger Binding="{Binding Role}" Value="Guest">
                        <Setter Property="Content" Value="You dont' have administration rights."></Setter>
                        <Setter Property="Background" Value="Red"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <ListBox Name="lstUsers">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="200"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <!--Bind the user name-->
                        <Label Grid.Column="0" Content="{Binding UserName}"></Label>

                        <!--Bind the address-->
                        <Label Grid.Column="1" Content="{Binding Address}"></Label>

                        <!--Apply the style resource for label-->
                        <Label Grid.Column="2" Style="{StaticResource lbStyle}"></Label>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

->Bind the listbox in the window load event.

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //User list
            List<User> lstUser = new List<User>();

            //Adding users.
            lstUser.Add(new User() { UserName = "David", Address = "Mumbai", Role = "Admin" });
            lstUser.Add(new User() { UserName = "Anderson", Address = "Pune", Role = "Guest" });
            lstUser.Add(new User() { UserName = "Andri", Address = "Mumbai", Role = "Admin" });

            //Set the item source here.
            lstUsers.ItemsSource = lstUser;
        }

Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…

sekhartechblog@gmail.com