Wednesday, May 16, 2012

Data Triggers in WPF

I just once required to color a row of Gridview on the basis of associated data
i used RowLoaded event of the grid
and applied the foreground color by applying check on the associated property like
GridDemo.DummyModel model = (GridDemo.DummyModel)e.Row.DataContext;
 if (model.AutoAdvance == true)
    {
     e.Row.Foreground = new SolidColorBrush(Colors.Orange);
    }

But it was not sufficient to handle run time changes in the binding source so i used
DataTriggers in xaml like
   
        I just once required to color a row of Gridview on the basis of associated data
i used RowLoaded event of the grid
and applied the foreground color by applying check on the associated property like

GridDemo.DummyModel model = (GridDemo.DummyModel)e.Row.DataContext;
 if (model.AutoAdvance == true)
    {
     e.Row.Foreground = new SolidColorBrush(Colors.Orange);
    }


But it was not sufficient to handle run time changes in the binding source so i used
DataTriggers in xaml like
        <UserControl.Resources>
        <Style TargetType="telerik:GridViewRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding AutoAdvance}" Value="true">
                    <Setter Property="Foreground" Value="Orange"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Succeeded}" Value="true">
                    <Setter Property="Foreground" Value="Blue"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Failed}" Value="true">
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

which changed the Foreground color of row to orange when binding values of AutoAdvance property =true
   

which changed the Foreground color of row to orange when binding values of AutoAdvance property =true

No comments:

Post a Comment