Question

My example below works fine, it highlights the cell in the Id column when the Id.Updated property is true.

I would like to know how to modify the binding expression Binding="{Binding Id.Updated}" in order to bind to the Updated property of the current IssueElement object, in the proper column (not only the Id one).

I would like to able to do this with only one style for all columns, and not one style per column.

The following example is a simplified version of how the DataGrid works in my application.

The DataGrid:

<DataGrid ItemsSource="{Binding IssueList}" AutoGenerateColumns="False" >
    <DataGrid.Resources>
        <Style x:Key="TestStyle" TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Id.Updated}" Value="True">
                    <Setter Property="Background" Value="Green" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>   
    <DataGrid.Columns>
        <DataGridTextColumn Header="Id" Binding="{Binding Id.Value}" CellStyle="{StaticResource TestStyle}" />
        <DataGridTextColumn Header="Title" Binding="{Binding Title.Value}" CellStyle="{StaticResource TestStyle}" />
        <DataGridTextColumn Header="Body" Binding="{Binding Body.Value}" CellStyle="{StaticResource TestStyle}" />
    </DataGrid.Columns>
</DataGrid>

The collection:

private ObservableCollection<Issue> mIssueList;
public ObservableCollection<Issue> IssueList
{
    get { return mIssueList; }
    set { mIssueList = value; OnPropertyChanged("IssueList"); }
}

The classes used by the collection

public class Issue
{
    public IssueElement Id { get; set; }
    public IssueElement Title { get; set; }
    public IssueElement Body { get; set; }
}

public class IssueElement
{
    public string Value { get; set; }
    public bool Updated { get; set; }
}

Thanks in advance

Was it helpful?

Solution

I'm not sure why you have One.Value, Two.Value, Three.Value in your bindings. I think you meant Id.Value, Title.Value and Body.Value, correct?

I think the only way you can do this is with a converter. Here is one way of doing it:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource UpdatedConverter}}" Value="True">
    <Setter Property="Background" Value="Green" />
</DataTrigger>

And the converter:

public class UpdatedConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGridCell dgc = value as DataGridCell;
        if (dgc != null)
        {
            Issue data = dgc.DataContext as Issue;
            if (data != null)
            {
                DataGridTextColumn t = dgc.Column as DataGridTextColumn;
                if (t != null)
                {
                    var binding = t.Binding as System.Windows.Data.Binding;
                    if (binding != null && binding.Path != null && binding.Path.Path != null)
                    {
                        string val = binding.Path.Path.ToLower();
                        if (val.StartsWith("id"))
                            return data.Id.Updated;
                        if (val.StartsWith("title"))
                            return data.Title.Updated;
                        if (val.StartsWith("body"))
                            return data.Body.Updated;
                    }
                }
            }
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top