下面的示例很好,当Id.Updated属性为true时,它会高亮显示Id列中的单元格。
我想知道如何修改绑定表达式Binding="{Binding Id.Updated}",以便绑定到当前IssueElement对象的Updated属性(不仅仅是Id列)。
我希望能够对所有列只使用一种样式,而不对每列使用一种样式。
下面的示例是DataGrid如何在我的应用程序中工作的简化版本。
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; }
}提前感谢
发布于 2014-05-13 14:32:58
我不知道为什么你的绑定中有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
}https://stackoverflow.com/questions/23631126
复制相似问题