【发布时间】:2021-11-16 14:31:50
【问题描述】:
我试图在互联网上搜索解决我的问题的想法,但我没有得到任何想法。
我有这些数据模板:
<DataTemplate x:Key="UnitGridCell">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<dxe:TextEdit EditValue="{Binding Row.Unit.NewValue}" Style="{StaticResource PartStyle}" />
<dxe:TextEdit EditValue="{Binding Row.Unit.OldValue}" Style="{StaticResource PartStyle}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="PreGridCell">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<dxe:TextEdit EditValue="{Binding Row.Pre.NewValue}" Style="{StaticResource PartStyle}" />
<dxe:TextEdit EditValue="{Binding Row.Pre.OldValue}" Style="{StaticResource PartStyle}" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="ExecutionGridCell">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<dxe:TextEdit EditValue="{Binding Row.Execution.NewValue}" Style="{StaticResource PartStyle}" />
<dxe:TextEdit EditValue="{Binding Row.Execution.OldValue}" Style="{StaticResource PartStyle}" />
</StackPanel>
</DataTemplate>
对于这些 GridColumn:
<Grid >
<dxg:GridControl ItemsSource="{Binding DataGridModels}" >
<dxg:GridColumn x:Name="UnitCol" FieldName="Unit" CellTemplate="{StaticResource UnitGridCell}"/>
<dxg:GridColumn x:Name="PreCol" FieldName="Pre" CellTemplate="{StaticResource PreGridCell}"/>
<dxg:GridColumn x:Name="ExCCol" FieldName="Execution" CellTemplate="{StaticResource ExecutionGridCell}"/>
问题是:如何进行通用绑定,以便为所有网格列创建一个 DataTemplate?
提前谢谢你!
【问题讨论】:
-
泛型不能解决您的问题,无论是在 C# 还是 XAML 中。因为您是通过 individual 嵌套属性路径引用源属性,所以您需要了解每种类型的特殊知识。显然,该操作不是通用的,因为每种情况都不同。如果您的 DataTemplate 没有指定 DataType 并且所有数据类型的绑定路径都相同,那么您可以将单个 DataTemplate 用于遵循具有相同绑定路径的约束的任何类型。您可以通过将相关的公共属性移动到基类来确保这一点。
-
在您的情况下,您必须创建一个源集合,例如对象类型,包含您在引用二级属性 Unit、Pre 和 Execution 时获得的所有实例。换句话说,您必须消除嵌套路径并直接绑定到 NewValue 和 OldValue。然后您可以使用单个 DataTemplate,但前提是所有绑定路径都针对公共属性并且未设置 DataTemplate.DataType 属性..
标签: c# wpf devexpress-wpf