Reputation: 468
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
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 theContent....
binding also.
Upvotes: 1
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