Change Cell Format of a WPF Gridcell based on the Value of an other Cell Value

239 Views Asked by At

I am starting to get involved with WPF MVVM. Now in a grid I need to format the appearance of a cell based on the value in another cell.

Current Grid Example 1

The values in 'SAP No.' should appear red if the value 'DoppelSAPOrder' is true in the viewmodel. The value 'DoppelSAPOrder' is not displayed in the grid.

The grid is on a UserControl

<UserControl...

    <UserControl.DataContext>
    <model:MainVM />
</UserControl.DataContext>

<Grid>
    <datagrid:ThemedDataGrid HorizontalAlignment="Stretch" Height="auto" VerticalAlignment="Stretch" Width="auto"
                             x:Name="DataGrid" ItemsSource="{Binding Tickets}" AutoGenerateColumns="False"
                             IsReadOnly="True"
                             ClipboardCopyMode="IncludeHeader" SelectedItem="{Binding SelectedTicket}"...

<datagrid:ThemedDataGrid.Columns>

            <datagrid:ThemedDataGrid.Columns>
            <DataGridTextColumn Binding="{Binding CrmTicketId}" Header="Ticket Nr." Width="90">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="HorizontalAlignment" Value="Center" />
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

            <DataGridTextColumn Binding="{Binding SapAuftrag}" Header="SAP Nr." Width="85">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="HorizontalAlignment" Value="Center" />
                        <Setter Property="Foreground" Value="{Binding ???, Converter={StaticResource SomeBoolToBrushConverter-ToDo}}"/>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>

The grid is bound to a List.

public class SimpleTicketDTO : BaseDTO
{
    public string Kategorie       { get; set; }
    public string Status          { get; set; }
    public string PersonFirmaName { get; set; }
    public bool   HatSAPOrder     { get; set; }
    public string SapAuftrag      { get; set; }
    public bool   DoppelSAPOrder { get; set; }

...

I cannot now access the property 'DoppelSAPOrder ' of the Row in the ElementStyle :-/

Can someone show me how to address this problem?

1

There are 1 best solutions below

0
mdziadowiec On

You want to bind TextBlock property in element style to its' parent row binded object and it can be achieved with the RelativeSource property of the binding like this:

<DataGridTextColumn Binding="{Binding SapAuftrag}" Header="SAP Nr." Width="85">
    <DataGridTextColumn.ElementStyle>
        <Style TargetType="{x:Type TextBlock}">
           <Setter Property="HorizontalAlignment" Value="Center" />
           <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}, Path =Item.DoppelSAPOrder,Converter={StaticResource IsObsoleteToTextDecorationsConverter}}"/>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>