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

PropertyTrigger In Wpf:-

We can change the property of control based on another property using property triggers.The below code is simple example of property trigger which display the text based on checkbox.

<Window x:Class="WpfPropertyTriggers.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>
        <Grid.Resources>
            <!--Add the style for check box-->
            <Style TargetType="{x:Type CheckBox}" x:Key="cbStyle">
                <Style.Triggers>
                    <!--Check whether checkbox checked or not-->
                    <Trigger Property="IsChecked" Value="true">
                        <!--If checkbox is checked then display the text and set the color-->
                        <Setter Property="Foreground" Value="Red"></Setter>
                        <Setter Property="Content" Value="You checked the box..."></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <!--Apply the style resource for check box-->
        <CheckBox Name="cb" Grid.Column="0" Style="{StaticResource cbStyle}"></CheckBox>
    </Grid>
</Window>



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

ObjectDataProvider In Wpf:-


We can get the objects in to the xaml by using ObjectDataProvider.We can aslo  call the functions from the xaml.

->Create a customer class with properties FirstName,LastName and create a      GetCustomer function which is called by the xaml.
   public class Customer
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public Customer GetCustomer()
        {
            return new Customer { FirstName = "Jim", LastName = "Smith" };
        }
    }

->Add the ObjectDataProvider in the xaml as static resource.
<Window x:Class="WpfCodeObjectToXaml.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfCodeObjectToXaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>
            <!--Here specify the type of object and method name-->
            <ObjectDataProvider x:Key="odpCustomer" ObjectType="{x:Type local:Customer}" MethodName="GetCustomer"></ObjectDataProvider>
        </Grid.Resources>
        <!--Here we set the datacontext of stack panel to the object data provicer-->
        <StackPanel DataContext="{StaticResource odpCustomer}">
            <!--Bind the first name-->
            <TextBlock Text="{Binding Path=FirstName}"></TextBlock>

            <!--Bind the last name-->
            <TextBlock Text="{Binding Path=LastName}"></TextBlock>
        </StackPanel>
    </Grid>
</Window>

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

IMultiValueConverter In Wpf:-


There may be scenorios like binding the label with FullName using FirstName and LastName propertys.So we can achieve this using  IMultiValueConverter.

->Create a User class.

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

        public string LastName { get; set; }
}


->Create a MultiConverter class and implement IMultiValueConverter methods.

public class MultiConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //Check the parameter.
            if (parameter.ToString() == "FullName")
            {
                //Return the fullname.
                return values[0] + values[1].ToString();
            }

            return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

->Add the MultiConverter class in xaml as static resource.
<Window x:Class="WpfIMultiValueConverter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WpfIMultiValueConverter"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Window.Resources>
        <my:MultiConverter x:Key="multiConverter"></my:MultiConverter>
    </Window.Resources>
    <Grid>
        <ListBox Name="lstUsers">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="{Binding Path=FirstName}"></TextBlock>
                        <TextBlock Grid.Column="1" Text="{Binding Path=LastName}"></TextBlock>
                        <TextBlock Grid.Column="2">
                           <TextBlock.Text>

                           <!--We should use MultiBinding to work with IMultiValueConverter-->
                                                                <MultiBinding Converter="{StaticResource multiConverter}" ConverterParameter="FullName">
                                    <Binding Path="FirstName"></Binding>
                                    <Binding Path="LastName"></Binding>
                                </MultiBinding>
                               </TextBlock.Text>
                        </TextBlock>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

->In window load event bind the listbox.
private void Window_Loaded(object sender, RoutedEventArgs e)
{

      //User list
      List<User> lstUser = new List<User>();

      //Adding users.
      lstUser.Add(new User() { FirstName = "David", LastName = "Jackson" });
      lstUser.Add(new User() { FirstName = "Anderson", LastName = "Andi" });
      lstUser.Add(new User() { FirstName = "Andri", LastName = "Jackson" });

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