将对象绑定到数据网格单元-条件单元样式 [英] Binding an object to data grid cell - conditional cell style
问题描述
TL; DR :如果
DataGrid
单元格绑定到特定对象(不是字符串,整数, ...),如何在转换器或设置器中访问它?
TL;DR: if a
DataGrid
cell is bound to a specific object (not string, int, ...), how can I access it in converter or setter?
完整版本:
我有一个 DataGrid
绑定到 DataTable
:
<Window x:Class="TabControlTests.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid x:Name="grid" ItemsSource="{Binding Data}" />
</Grid>
</Window>
DataTable
是动态生成的(实际上是生活代码,我不知道列名):
DataTable
is generated dynamically (and in real life code I don't know the column names):
public partial class MainWindow : Window
{
public DataTable Data { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
SetDataTable();
}
private void SetDataTable()
{
var dt = new DataTable();
var name = new DataColumn("Name", typeof(object));
var age = new DataColumn("Age", typeof(int));
var home = new DataColumn("Home", typeof(string));
dt.Columns.Add(name);
dt.Columns.Add(age);
dt.Columns.Add(home);
AddRow(dt, "Kavitha Reddy", 15, "NY");
AddRow(dt, "Kiran Reddy", 24, "LA");
AddRow(dt, "John Doe", 55, "TX");
Data = dt;
}
private void AddRow(DataTable dt, string name, int age, string home)
{
var dr = dt.NewRow();
dr["Name"] = new Person(name, age >= 18);
dr["Age"] = age;
dr["Home"] = home;
dt.Rows.Add(dr);
}
}
}
注意,对于 Name
列,我分配一个对象,而不是字符串。由于已实现 ToString()
方法,因此显示名称:
Note that for Name
column I assign an object, not string. As it has ToString()
method implemented it displays the name:
public class Person
{
public string Name { get; set; }
public bool IsAdult { get; set; }
public Person(string name, bool isAdult)
{
Name = name;
IsAdult = isAdult;
}
public override string ToString()
{
return Name;
}
}
如何在<$每个具有 IsAdult ==假
的人的c $ c>名称列?我尝试使用一些转换器,但无法在任何地方访问 Person
对象,我得到的只是 DataGridCell
实例。
How can I display a different background in the Name
column for each person that has IsAdult == false
? I tried to use some converters but I cannot access the Person
object anywhere, all I got was DataGridCell
instance.
编辑
这是我尝试使用转换器的方式:
This is how I tried to use a converter:
<Window.Resources>
<local:ValueToBrushConverter x:Key="ValueToBrushConverter" />
<Style x:Key="CellStyle" TargetType="DataGridCell">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ValueToBrushConverter}}" />
</Style>
</Window.Resources>
<Grid>
<DataGrid x:Name="grid" ItemsSource="{Binding Data}" CellStyle="{StaticResource CellStyle}" />
</Grid>
推荐答案
您不能真正存储<$ c $ DataRow
中的c> Person 对象。您可以自己确认:
You can't really store the Person
object in the DataRow
. You can confirm this yourself:
var dr = dt.NewRow();
//set the Name column to a Person object
dr["Name"] = new Person(name, age >= 18);
//...and retrieve it. It is now a string:
var thePerson = dr["Name"] as string;
使用 DataTable
混合对象模型是这不是一个好主意。您应该将 DataTable
替换为自定义对象的集合。然后,您将可以使用值转换器或 DataTrigger
设置单元格的 Background
属性。
Mixing an object model with a DataTable
is not a good idea. You should replace the DataTable
with a collection of custom objects. Then you will be able to use a value converter or a DataTrigger
to set the Background
property of the cell.
这篇关于将对象绑定到数据网格单元-条件单元样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!