我的下面示例工作正常,当Id.Updated
属性为true时,它突出显示Id列中的单元格。
我想知道如何修改绑定表达式Binding="{Binding Id.Updated}"
,以便在正确的列(不仅是Id)中绑定到当前Updated
对象的IssueElement
属性一个)。
我希望能够为所有列只使用一种样式,而不是每列一种样式。
以下示例是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; }
}
提前致谢
答案 0 :(得分:2)
我不确定为什么你的绑定中有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
}