Question

I have got following XAML to change Cell.

<sdk:DataGrid  Margin="10" Grid.Row="1" IsReadOnly="True" AutoGenerateColumns="False" x:Name="dgrdDataGrid" GridLinesVisibility="All">
              <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" />
                <sdk:DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" />
                <sdk:DataGridTemplateColumn Header="Subscribed" Width="*" SortMemberPath="IsSubscribed">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" >
                                <TextBlock  Text="No Information" x:Name="txtTextBlock" Width="100" VerticalAlignment="Center" Margin="5"/>
                                <i:Interaction.Triggers>
                                    <ei:DataTrigger  Binding="{Binding Path=IsSubscribed}" Value="true">

                                        <ei:ChangePropertyAction TargetObject="{Binding ElementName=txtTextBlock}"  TargetName="Text" Value="Verified" PropertyName="Text"/>
                                        <ei:ChangePropertyAction TargetObject="{Binding ElementName=txtTextBlock}"  TargetName="Foreground" Value="Red" PropertyName="Foreground"/>
                                        <ei:ChangePropertyAction TargetObject="{Binding ElementName=txtTextBlock}"  TargetName="Foreground" PropertyName="FontWeight">
                                            <ei:ChangePropertyAction.Value>
                                                <FontWeight>Bold</FontWeight>
                                            </ei:ChangePropertyAction.Value>
                                        </ei:ChangePropertyAction>
                                    </ei:DataTrigger>
                                    <ei:DataTrigger  Binding="{Binding Path=IsSubscribed}" Value="false">
                                        <ei:ChangePropertyAction TargetObject="{Binding ElementName=txtTextBlock}"  TargetName="Text" Value="Not Verified" PropertyName="Text"/>
                                    </ei:DataTrigger>
                                </i:Interaction.Triggers>
                            </StackPanel>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>

Now I want to change background of the Rows that are IsSubscribed = true, let say use Yellow color.

Is it possible to do it somehow based on XAML I have got?

Was it helpful?

Solution

Well if you are not following MVVM, Add an event like below

 void dgrdDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
    DataGridRow row = e.Row;
    var c = row.DataContext as YourDataGridOjbect;         
    if (c != null && c.IsSubscribed == true)
        e.Row.Foreground = new SolidColorBrush(Colors.Yellow);
    else
        e.Row.Foreground = new SolidColorBrush(Colors.Red);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top