Monday 2 April 2012

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 { getset; }
        public string Address { getset; }
        public string Role { getset; }
        public string Designation { getset; }
 }

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

No comments:

Post a Comment