简体   繁体   English

WPF DataGrid从DataGridCell样式绑定到当前对象属性

[英]WPF DataGrid binding to current object property from DataGridCell style

My example below works fine, it highlights the cell in the Id column when the Id.Updated property is true. 我下面的示例工作正常,当Id.Updated属性为true时,它将突出显示Id列中的单元格。

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). 我想知道如何修改绑定表达式Binding="{Binding Id.Updated}" ,以便在适当的列(不仅是Id)中绑定到当前IssueElement对象的Updated属性。

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. 下面的示例是DataGrid在我的应用程序中的工作方式的简化版本。

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 提前致谢

I'm not sure why you have One.Value, Two.Value, Three.Value in your bindings. 我不确定为什么您的绑定中包含One.Value,Two.Value,Three.Value。 I think you meant Id.Value, Title.Value and Body.Value, correct? 我认为您的意思是Id.Value,Title.Value和Body.Value,对吗?

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM