I have a list of objects implementing INotifyPropertyChange bound to a Grid (particularly CSLA objects). One of the properties is called IsValid and responds true if there is any error on the object.
I've applied the following style
<Style TargetType="{x:Type igDP:DataRecordCellArea}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Record.DataItem.IsValid, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Value="False">
<Setter Property="Background" Value="Red" />
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Record.DataItem.IsValid}" Value="True">
<Setter Property="Background" Value="Green" />
</Style.Triggers>
</Style>
I also have a textbox outside of the grid bound to a field of the active record. When I put invalid data in this field, I can see the grid update with the same info, but it doesn't trigger the red background. If I scroll the record out of view and back into view, it then displays a red background. How do I set it up to display the red background when the data becomes invalid?
This is pretty much a deal breaker for me. I guess I will continue to use the Xceed datagrid until this can get resolved. It's too bad too. I like the feel of this grid better than Xceed.
Why can't you just use data triggers? They work just fine. The style selector (from what I've deduced) was intended for the initial selection, not to be dynamic.
But again, it's super easy to just throw a trigger on the template where you do anything you want - it's core WPF, just as you'd do with any other control.
I believe I am using datatriggers in my example above. What is it I should throw a trigger on? Can you give an example.
I appreciate your attempt to help, but I am editing data from outside the grid in stand alone controls below the grid. Your code does not work in this situation, but behaves the same as my code at the top.
Please scratch this last comment
Thank you VERY much. I will try this out.
Strangely, I tried to post a response to this, along with a working sample and got a "moderated forum" message - something I had never seen here before. What you are trying to do is quite simple. Maybe my other message will appear tomorrow? In the meantime, here is the xaml portion that should demonstrate the idea:
<Window x:Class="XamDataGrid_Test_of_Dynamic_Style.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:igDP="http://infragistics.com/DataPresenter" xmlns:local="XamDataGrid_Test_of_Dynamic_Style" Title="Window1" Height="300" Width="300"> <Grid> <Grid.Resources> <Style x:Key="ManagerStyle" TargetType="{x:Type igDP:CellValuePresenter}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=(igDP:DataRecord.DataItem).IsManager}" Value="True"> <Setter Property="Background" Value="Red" /> </DataTrigger> </Style.Triggers> </Style> </Grid.Resources>
<StackPanel Orientation="Vertical">
<Button Name="ToggleManager" Click="ToggleManager_Click" Margin="20">Toggle Manager</Button>
<igDP:XamDataGrid Name="TestGrid" AutoFit="True" GroupByAreaLocation="None">
<igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout>
<igDP:FieldLayout.Fields> <igDP:Field Name="Name"> <igDP:Field.Settings> <igDP:FieldSettings CellValuePresenterStyle="{StaticResource ManagerStyle}" /> </igDP:Field.Settings> </igDP:Field>
<igDP:Field Name="IsManager" /> </igDP:FieldLayout.Fields>
<igDP:FieldLayout.SortedFields> <igDP:FieldSortDescription FieldName="Name" Direction="Ascending" IsGroupBy="False" /> </igDP:FieldLayout.SortedFields> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
</StackPanel>
</Grid></Window>
Ok, I whipped up a small test app that seems to work just fine for me. Here is the xaml, but attached is a zip file with everything.
If you click on the button, it will set the IsManager property of the second person in the bound collection. The style will correctly change the background color.
</igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts>
I misunderstood. I read "dynamic style" and for some reason my brain brought me down the road of style selectors, even though your example clearly shows data triggers. Sorry.
You should be able to do what you are looking for. Unless I'm missing something, I've done exactly this. For example, I just dug out the code below from an app that shows an icon under certain conditions.
<ControlTemplate.Triggers> <DataTrigger Binding="{Binding Path=DataItem, Converter={StaticResource hasUnscrubbedDataConverter} }" Value="false"> <Setter Property="Visibility" TargetName="Image" Value="Visible" /> </DataTrigger></ControlTemplate.Triggers>
Maybe my memory is bad and that only handles the initial styling? It's interesting that I'm binding to the DataItem and passing that into the converter. How could his get re-evaluated if it's bound to the data item itself, as opposed to a notifyable property? I will try to whip up a small sample project to do some testing.