wpfxamlmaster-detailxceedxceed-datagrid

Xceed WPF DataGrid - Master/Detail : Change background color of cell in detail section based on data


I'm new to WPF which might explain the difficulty I'm having doing this. As an example of what I want to do, I'm using a simplified version of Xceed's MasterDetial sample app which has Employees as a Master and the Orders associated with each employee as Details. The DetailConfigurations are working. I'm using this example because I need to get the same functionality working in a much larger and more complicated application.

Where I'm running into trouble is trying to change the background color of a single DataCell in the detail section. By way of example, say I've expanded the first master (employee) row and get back a list of orders. Each order has a ShipCountry field. If the value of ShipCountry is "Poland", I want to change the background of the ShipCountry cell (and ONLY that cell) to Red.

The below does it for the entire row, despite there being a target type of DataCell. I can't figure out why this would be the case. I've tried any number of different approaches based on things I've found in searching for this problem but none have worked. I figure I'm missing something obvious and it's a simple binding issue but that's where being a newbie to WPF (and to the Xceed grid) is hindering me.

Any help would be greatly appreciated!

<xcdg:DataGridControl
     x:Name="grid"
     AllowDetailToggle="{Binding Source={x:Static local:MainPageParams.Singleton},Path=AllowDetailToggle}"
     AutoCreateDetailConfigurations="False"
     CellEditorDisplayConditions="None"
     EditTriggers="BeginEditCommand,ActivationGesture,ClickOnCurrentCell"
     ItemScrollingBehavior="Immediate"
     ItemsSource="{Binding Source={StaticResource cvsEmployees}}">
        <xcdg:DataGridControl.Resources>
            <Style TargetType="{x:Type xcdg:DataCell}">
                <Style.Triggers>

                    <!-- Fieldname not a valid property...
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding Path=ShipCountry}" Value="Poland"/>
                            <Condition Binding="{Binding Self, Path=FieldName}" Value="ShipCountry"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Background" Value="Red"/>
                    </MultiDataTrigger>
                    -->

                    <!-- This changes the entire row to Red, not just the ShipCountry field-->
                    <DataTrigger Binding="{Binding Path=ShipCountry}" Value="Poland">
                        <Setter Property="Background" Value="Red"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </xcdg:DataGridControl.Resources>
        <xcdg:DataGridControl.View>

...


Solution

  • I think the issue with your first example is:

    Binding="{Binding Self, Path=FieldName}"
    

    This is what I do on mine. I just swapped to using your parameters.

    <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
            <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=FieldName}" Value="ShipCountry" />
            <Condition Binding="{Binding ShipCountry}" Value="Poland" />
        </MultiDataTrigger.Conditions>
        <Setter Property="Background" Value="Red" />
    </MultiDataTrigger>
    

    Or another way I do it is in the column's datatemplate:

    Column Declaration:

    <xcdg:Column Title="Ship Country"
        CellContentTemplate="{StaticResource ShipCountryDataTemplate}"
        FieldName="ShipCountry" />
    

    DataTemplate

        <DataTemplate x:Key="ShipCountryDataTemplate" DataType="{x:Type dat:Order}">
            <TextBlock x:Name="txt" 
                Text="{Binding}" />
            <DataTemplate.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=xcdg:DataRow, AncestorLevel=1}, Path=DataContext.EmployeeChanged, Mode=OneWay}" Value="True">
                    <Setter TargetName="txt" Property="Background" Value="Red" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>