David Graham
David Graham

Reputation: 468

WPF bind DataGridTextColumns Cell string value into style setter

I have a datagrid bound to a list and each cells value is bound to unique booleans.

<UserControl.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Have1}" Value="True">
                <Setter Property="Background" Value="#263DDE36"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Have1}" Value="False">
                <Setter Property="Background" Value="#26FF5454"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

<DataGrid ItemsSource="{Binding AuditList}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="Art1" Binding="{Binding Have1}"/>
        <DataGridTextColumn Header="Art2" Binding="{Binding Have2}"/>
        <DataGridTextColumn Header="Art2" Binding="{Binding Have3}"/>
    </DataGrid.Columns>
</DataGrid>

This works fine but I would like to change the binding "Have1", to the cells current string or bool value.

<DataTrigger Binding="{Binding DataCellsValue="True"}" Value="True">
    <Setter Property="Background" Value="#263DDE36"/>
</DataTrigger>
<DataTrigger Binding="{Binding DataCellsValue="False"}" Value="False">
    <Setter Property="Background" Value="#26FF5454"/>
</DataTrigger>

How is this achieved? Ideally, I just want to use the one setter for all columns without duplicating setters for each columns cell.

Thanks!

Upvotes: 1

Views: 2210

Answers (2)

Kylo Ren
Kylo Ren

Reputation: 8823

Use the DataTrigger as below:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=Content.Text}" Value="1">
     <Setter Property="Background" Value="#263DDE36"/>
</DataTrigger>

Now as long as you are not defining DataGridCell's ControlTemplate above binding will work fine. If you ever define Template then change the Content.... binding also.

Upvotes: 1

Felix D.
Felix D.

Reputation: 5123

Try this for your Bindings:

{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}}

For the Path use the PropertyName of the Value of the Cell .. Not sure if its Value/Text/Content...

See How do I use WPF bindings with RelativeSource? for details

EDIT:

The Path should be Content. So the complete solution would be:

<UserControl.Resources>
<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=Content, 
                     RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}}" 
                     Value="True">
            <Setter Property="Background" Value="#263DDE36"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=Content, 
                     RelativeSource={RelativeSource AncestorType={x:Type DataGridCell}}}"  
                     Value="False">
            <Setter Property="Background" Value="#26FF5454"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

Upvotes: 1

Related Questions