0

I'm still having some problems getting the hang of WPF. I feel that this is probably a simple question, and I'm wondering why I can 't get this to work like I think it should.

I'm trying to use a DataTrigger to show that a value in my DataGrid is "dirty," meaning that its value has changed since the data was last committed to the database. The items bound to my grid view have an IsDirty property that is working fine. I'm trying to use a red asterisk in a TextBlock in the cell in the grid to indicate that there is an uncommitted change, using a DataTrigger, like this, in my DataGrid.Resources. The idea is that the font color would change from Transparent to Red if IsDirty on the selected row item is True:

            <DataTemplate x:Key="CleanTemplate">
                <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Column="0" Text="*" Padding="0,0,5,0" Foreground="Transparent" FontWeight="Bold" FontFamily="Lucida Console" >
                        <TextBlock.Style>
                            <Style TargetType="TextBlock">
                                <Style.Triggers>
                                    <!-- Not sure why this isn't working... this text block can't see changes in IsDirty for some reason-->
                                    <DataTrigger Binding="{Binding Path=IsDirty}" Value="True">
                                        <Setter Property="Foreground" Value="Red" />
                                    </DataTrigger>                                                                                                                    
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                    <Label Grid.Column="1" Foreground="DarkGray" Background="Transparent" VerticalContentAlignment="Center" Content="{Binding Path=Value,Mode=OneWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" />
                </Grid>
            </DataTemplate>

My guess is that somehow the DataContext of the Style Trigger is different from the DataContext for the actual TextBlock (and the Label which also part of the cell in the DataGrid), and I need to change the Binding Path so that the trigger can "see" the selected item and its properties. I'm not sure how I can navigate to where it can see the selected item's properties in the grid.

I was able to get it to work by a CellTemplateSelector on the DataGridTemplateColumn, but the customer is requesting different input controls based on the type of data for the selected row, so now the criteria for my data template selection is getting more complicated (now I need to have a template for each separate control and one that is basically the same, except showing the value IsDirty).

XAML:

    <DataGridTemplateColumn
        Header="Value"
        Width="*"
        MinWidth="25"
    >
        <DataGridTemplateColumn.CellTemplateSelector>
            <local:ParameterStateTemplateSelector 
                CleanTemplate="{StaticResource CleanTemplate}"
                CleanTemplateYN="{StaticResource CleanTemplateYN}"
                DirtyTemplate="{StaticResource DirtyTemplate}"
                DirtyTemplateYN="{StaticResource DirtyTemplateYN}"
            />
        </DataGridTemplateColumn.CellTemplateSelector>
    </DataGridTemplateColumn>

Templates for the Label type data (the difference is that the Foreground of the TextBlock is Transparent or Red depending on IsDirty):

    <DataTemplate x:Key="CleanTemplate">
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Text="*" Padding="0,0,5,0" Foreground="Transparent" FontWeight="Bold" FontFamily="Lucida Console" />
            <Label Grid.Column="1" Foreground="DarkGray" Background="Transparent" VerticalContentAlignment="Center" Content="{Binding Path=Value,Mode=OneWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" />
        </Grid>
    </DataTemplate>

    <DataTemplate x:Key="DirtyTemplate">
        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <TextBlock Grid.Column="0" Text="*" Padding="0,0,5,0" Foreground="Red" FontWeight="Bold" FontFamily="Lucida Console"/>
            <Label Grid.Column="1" Foreground="DarkGray" Background="Transparent" VerticalContentAlignment="Center" Content="{Binding Path=Value,Mode=OneWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" />
        </Grid>
    </DataTemplate>

Code behind for the DataTemplateSelector (seems like a DataTrigger for IsDirty would be simpler):

public class ParameterStateTemplateSelector : DataTemplateSelector
{
    public DataTemplate CleanTemplate { get; set; }
    public DataTemplate CleanTemplateYN { get; set; }
    public DataTemplate DirtyTemplate { get; set; }
    public DataTemplate DirtyTemplateYN { get; set; }
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        ScenarioParam selectedItem = (ScenarioParam)item;
            if (selectedItem != null)
        {
            if (selectedItem.ParamObject.DataType == "BOOL")
            {
                if (selectedItem.IsDirty)
                {
                    return DirtyTemplateYN;
                }
                else
                {
                    return CleanTemplateYN;
                }
            }
            else
            {
                if (selectedItem.IsDirty)
                {
                    return DirtyTemplate;
                }
                else
                {
                    return CleanTemplate;
                }
            }
        }
        return base.SelectTemplate(item, container);
    }
}

Any suggestions on how I can get the DataTrigger to "see" the changes in the selected item's properties? Or maybe it just isn't possible to use DataTriggers in the way I would like?

Thanks!

7
  • First question, does your IsDirty property fire a property change notification? Feb 11, 2013 at 15:58
  • Thanks, good question... yes, it does, although that code is in a separate library that I don't control. Originally I had a separate DataGridCheckboxColumn that was bound to IsDirty, and it would update correctly when the Value was changed. Feb 11, 2013 at 16:09
  • Ok, do you have binding debugging messages enabled in visual studio? If so, are there any messages related to binding errors? Feb 11, 2013 at 16:13
  • I wasn't familiar with this setting, but I found it after looking here: msdn.microsoft.com/en-us/library/dd409960(v=vs.100).aspx The Data Binding node was set to Error, so I think that should have gotten it, but I changed it to Warning just in case. I didn't see anything in my Output window before or after changing the setting. I did some edits in my grid which I think should have caused the trigger to fire. Feb 11, 2013 at 17:24
  • I changed the binding the IsDirty property to "IsDirtier" which doesn't exist, and now I'm seeing errors for each item in my grid, so I assume that it can see IsDirty. I wonder if IsDirty isn't sending a change notification like you suggested. Is there a way to listen for these events for debugging? BindingExpression path error: 'IsDirtier' property not found on 'object' ''ScenarioParam' ... BindingExpression:Path=IsDirtier; DataItem='ScenarioParam' ...; target element is 'TextBlock' (Name=''); target property is 'NoTarget' (type 'Object') Feb 11, 2013 at 18:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.