为 DataGridTemplateColumn 指定数据上下文
Specifying data context for DataGridTemplateColumn
DataGrid 绑定到 List,其中 T 有几个嵌套属性(以及其他属性)。
class T
{
public X PropertyX {get;}
public Y PropertyY {get;}
public Z PropertyZ {get;}
}
class X { public A SomeProperty {get;}}
class Y { public A SomeProperty {get;}}
class Z { public A SomeProperty {get;}}
X、Y 和 Z 类 具有相同的 属性 类型 A 的 SomeProperty。
我需要分别显示 PropertyX、PropertyY 和 PropertyZ 的 SomeProperty 的数据。
所以,我需要这样的东西:
<DataGridTemplateColumn DataContext="{Binding X.A}" CellTemplate="{StaticResource CommonTemplate}" />
<DataGridTemplateColumn DataContext="{Binding Y.A}" CellTemplate="{StaticResource CommonTemplate}" />
<DataGridTemplateColumn DataContext="{Binding Z.A}" CellTemplate="{StaticResource CommonTemplate}" />
显然 DataGridTemplate 列没有 DataContext,所以我想知道这可能吗? CommonTemplate比较大,想复用一下
有什么想法吗?
您可以在另一个 DataTemplate 中重用现有的 DataTemplate,该 DataTemplate 还指定了 DataContext:
<DataGridTemplateColumn Header="x">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding PropertyX.SomeProperty}"
ContentTemplate="{StaticResource CellTemplate}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="y">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding PropertyY.SomeProperty}"
ContentTemplate="{StaticResource CellTemplate}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
DataGrid 绑定到 List,其中 T 有几个嵌套属性(以及其他属性)。
class T
{
public X PropertyX {get;}
public Y PropertyY {get;}
public Z PropertyZ {get;}
}
class X { public A SomeProperty {get;}}
class Y { public A SomeProperty {get;}}
class Z { public A SomeProperty {get;}}
X、Y 和 Z 类 具有相同的 属性 类型 A 的 SomeProperty。
我需要分别显示 PropertyX、PropertyY 和 PropertyZ 的 SomeProperty 的数据。
所以,我需要这样的东西:
<DataGridTemplateColumn DataContext="{Binding X.A}" CellTemplate="{StaticResource CommonTemplate}" />
<DataGridTemplateColumn DataContext="{Binding Y.A}" CellTemplate="{StaticResource CommonTemplate}" />
<DataGridTemplateColumn DataContext="{Binding Z.A}" CellTemplate="{StaticResource CommonTemplate}" />
显然 DataGridTemplate 列没有 DataContext,所以我想知道这可能吗? CommonTemplate比较大,想复用一下
有什么想法吗?
您可以在另一个 DataTemplate 中重用现有的 DataTemplate,该 DataTemplate 还指定了 DataContext:
<DataGridTemplateColumn Header="x">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding PropertyX.SomeProperty}"
ContentTemplate="{StaticResource CellTemplate}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="y">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding PropertyY.SomeProperty}"
ContentTemplate="{StaticResource CellTemplate}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>