首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >WPF DataGrid从DataGridCell样式绑定到当前对象属性

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

Stack Overflow用户
提问于 2014-05-13 12:21:45
回答 1查看 2.4K关注 0票数 1

下面的示例很好,当Id.Updated属性为true时,它会高亮显示Id列中的单元格。

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

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

下面的示例是DataGrid如何在我的应用程序中工作的简化版本。

DataGrid:

代码语言:javascript
运行
复制
<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>

收藏:

代码语言:javascript
运行
复制
private ObservableCollection<Issue> mIssueList;
public ObservableCollection<Issue> IssueList
{
    get { return mIssueList; }
    set { mIssueList = value; OnPropertyChanged("IssueList"); }
}

集合使用的类。

代码语言:javascript
运行
复制
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; }
}

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-13 14:32:58

我不知道为什么你的绑定中有One.Value,Two.Value,Three.Value。我想你是说Id.Value,Title.Value和Body.Value,对吧?

我想你唯一能做到的就是用转换器。这里有一种方法:

代码语言:javascript
运行
复制
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource UpdatedConverter}}" Value="True">
    <Setter Property="Background" Value="Green" />
</DataTrigger>

转换器:

代码语言:javascript
运行
复制
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
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23631126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档