Style DataGridRow WPF

1

I have a DataGrid with a DataGridTemplateColumn , a other columns that are loaded dynamically, I'm trying to change the background when the property Eliminado is in true , but it has not worked for me way.

The DataGrid is as follows.

<DataGrid Grid.Row="1" VerticalContentAlignment="Center" Name="gridDataUserControl" ItemsSource="{Binding DataSource}" Style="{DynamicResource estilodatagrid}" EnableColumnVirtualization="True"
                  CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="True"
                  AutoGenerateColumns="False" Padding="0,0,3,0" IsReadOnly="True" CellStyle="{DynamicResource estilodatagridcell}" RowStyle="{StaticResource estilodatagridrow}"
                  extension:DataGridColumnExtension.Columns="{Binding Columnas}">
            <DataGrid.Resources>
                <DataTemplate x:Key="HeaderCheckbox">
                    <CheckBox Name="SelectAll" IsEnabled="False" VerticalAlignment="Center" IsChecked="{Binding  RelativeSource={RelativeSource  Mode=FindAncestor, AncestorType=DataGrid}, Path=Tag, Mode=TwoWay}" Checked="DataGridCheckBoxColumnChecked" Unchecked="DataGridCheckBoxColumnChecked" />
                </DataTemplate>
                <DataTemplate x:Key="ItemCheckbox">
                    <CheckBox  VerticalAlignment="Center" IsChecked="{Binding Path=Seleccionado, Mode=TwoWay}" Checked="DataGridCheckBoxColumnChecked" Unchecked="DataGridCheckBoxColumnChecked" />
                </DataTemplate>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTemplateColumn Visibility="{Binding VerColumnaCheck}" x:Name="CheckAll"  HeaderTemplate="{StaticResource HeaderCheckbox}" CellTemplate="{StaticResource ItemCheckbox}">
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

And the estilodatagridrow style is

<Style x:Key="estilodatagridrow" TargetType="{x:Type DataGridRow}">
        <Setter Property="Background" Value="Coral" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DataGridRow">
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding Eliminado}" Value="True">
                            <Setter Property="Background" Value="Red" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <DataTrigger Binding="{Binding Eliminado}" Value="True">
                <Setter Property="Background" Value="Yellow"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    
asked by Jairo1010 18.11.2016 в 16:28
source

1 answer

1

First of all, there is something you have to check, and that is that your 'Model' and your 'ViewModel', implement INotifyPropertyChanged, which I suppose is a basic rule and you will have verified it.

Secondly, there is something that I find very strange in your RowStyle, and that is that you apply the DataTrigger twice, the first in the Template (something not too successful, since you do not modify anything of it) and a second in the style itself. What's more confusing is that you do it to paint the row of different colors.

The point is that I think that removing the Template part in the style should work.

I show you a very simple example that works even at run time:

I add the XALM code so you can see it more closely:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        DataContext="{Binding Source={StaticResource viewModel}}"
        Title="MainWindow" Height="387.333" Width="696.333">
    <Grid>
        <DataGrid ItemsSource="{Binding Datos}" AutoGenerateColumns="True">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding PropiedadBool}" Value="true">
                            <Setter Property="Background" Value="Green"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Grid>
</Window>

I hope it helps you.

    
answered by 19.11.2016 / 12:35
source