【问题标题】:WPF DataGrid binding to current object property from DataGridCell styleWPF DataGrid 从 DataGridCell 样式绑定到当前对象属性
【发布时间】:2014-05-13 12:21:45
【问题描述】:

下面的示例运行良好,当 Id.Updated 属性为 true 时,它​​会突出显示 Id 列中的单元格。

我想知道如何修改绑定表达式Binding="{Binding Id.Updated}",以便绑定到正确列中当前IssueElement 对象的Updated 属性(不仅仅是Id 之一)。

我希望所有列只使用一种样式,而不是每列一种样式。

以下示例是 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>

收藏:

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

集合使用的类

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; }
}

提前致谢

【问题讨论】:

    标签: c# wpf data-binding wpfdatagrid


    【解决方案1】:

    我不确定为什么绑定中有 One.Value、Two.Value、Three.Value。我认为您的意思是 Id.Value、Title.Value 和 Body.Value,对吗?

    我认为您可以做到这一点的唯一方法是使用转换器。 这是一种方法:

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

    还有转换器:

    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
    }
    

    【讨论】:

    • 谢谢,你说的一二三是对的,我忘记在发帖前更改变量名。我会测试你的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2014-08-15
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    相关资源
    最近更新 更多