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

No comments:

Post a Comment