1

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!

David Graham
  • 389
  • 5
  • 15

2 Answers2

1

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>

Community
  • 1
  • 1
Felix D.
  • 4,811
  • 8
  • 38
  • 72
  • Thanks for the link. I do need to take a deeper look at this, it will come in handy. Does that code work on your end? I couldn't get it to work at all there. The actual column for the cell is a DataGridTextColumn. How I used to do this was duplicating lots of code but each column is wrapped in a CellStyle. Does this even matter? – David Graham Feb 11 '16 at 07:37
  • Can you add some more code of your grid so I can reproduce it... It shall work ... xD – Felix D. Feb 11 '16 at 08:13
  • Thankyou. Included some more datagrid, but that really is it! – David Graham Feb 11 '16 at 08:25
  • If it's a simple value type being bound to the datagridcell then `...` should be sufficient, no need for a relative binding. – slugster Feb 11 '16 at 08:49
  • Thanks for the shorter syntax, but still no joy there. The binding is fine but not the color. The list really is just a class of booleans, for the view its displayed in a ListCollectionView. – David Graham Feb 11 '16 at 09:03
  • @horseman1210 I'm a bit puzzled - do you mean the object properties `Have1` and `Have2` etc. contain explicit string values "true" and "false", and you need to evaluate those as booleans in the data triggers? – slugster Feb 11 '16 at 09:12
  • Have1 is a bool. They display in the datagrid as True or False. You cannot edit these cells as a string because validation fails if you try any other value than T or F. Whether its bool or string, how to get that into setter without two A4 pages of Xaml? – David Graham Feb 11 '16 at 09:27
  • @horseman1210 If you are looking to make the cells editable, then use a [ValueConverter](https://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter%28v=vs.110%29.aspx) in the binding, and make sure you implement the [ConvertBack](https://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.convertback%28v=vs.110%29.aspx) method. – slugster Feb 11 '16 at 09:58
  • @slugster Thanks, you know I've never used that before :). I'll certainly be trying this soon, but still, is it needed? If I don't need the cells edited but just the style? At this rate I probably will just go back to 1 huge page of setters. After all, that does get the job done , but not good for reading. – David Graham Feb 11 '16 at 10:05
  • 1
    @slugster just one point. will not work here cause DataGridCell gets whole item as DataContext. – Kylo Ren Feb 12 '16 at 22:19
1

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.

Kylo Ren
  • 8,551
  • 6
  • 41
  • 66
  • Thanks, that's exactly what was needed! Had to use "True" or "False" in the binding value tag. I only mention this because I wasn't sure if 1 or 0 is supposed to work as bool values like you have in example. Thankyou. – David Graham Feb 13 '16 at 01:41
  • @horseman1210 i was working was integer column so i put 1 there. sorry for that. – Kylo Ren Feb 13 '16 at 04:24
  • down-voting people please add a comment too. – Kylo Ren Feb 19 '16 at 09:34