我有这个DataGridRow模板(它已经简化):
<Style TargetType="{x:Type DataGridRow}" x:Key="DataGridRowStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Grid>
<Border x:Name="OverlayBorder" Opacity="0.08">
<Border.Background>
<LinearGradientBrush EndPoint="0,0" StartPoint="10,10"
MappingMode="Absolute" SpreadMethod="Repeat">
<GradientStop Color="{Binding EntityState, Converter={resources:EntityStateToColorConverter}}" Offset="0" />
<GradientStop Color="{Binding EntityState, Converter={resources:EntityStateToColorConverter}}" Offset="0.4" />
<GradientStop Color="Transparent" Offset="0.4" />
<GradientStop Color="Transparent" Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
<Border x:Name="DGR_Border"
Background="{TemplateBinding Background}">
<DataGridCellsPresenter />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource DataGridRowBackgroundHover}" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource DataGridRowBackgroundSelected}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我有一些课程:
public class EntityWithState
{
public EntityStateEnum EntityState { get; set; }
}
该模板适用于类EntityWithState
。看看这一行
<GradientStop Color="{Binding EntityState, Converter={resources:EntityStateToColorConverter}}" Offset="0" />
取决于EntityWithState.EntityState
DataGridRow设置渐变背景。
但如果我有另一个班级:
public class EntityWithEntityWithState
{
public EntityWithState EntityWithState { get; set; }
}
我们的模板必须是:
<GradientStop Color="{Binding EntityWithState.EntityState, Converter={resources:EntityStateToColorConverter}}" Offset="0" />``
我的问题是如何在几个课程中使用相同的模板? 我认为Attached Propery帮助了我。例如:
<DataGrid UiExtensions:DataGrid.StatePropName="EntityWithState.EntityState">
...
</DataGrid>
但我不知道如何实现它。或者,如果有另一种解决方案......
更新(22.11.2017)
我可以用
public class EntityWithEntityWithStateViewModel
{
public EntityWithEntityWithState EntityWithEntityWithState { get; set; }
public EntityStateEnum EntityState => EntityWithEntityWithState.EntityWithState.EntityState;
}
但我希望在没有ViewModel访问的情况下进行,只能使用XAML和UI
更新2 (23.11.2017)
换句话说,我需要像DisplayMemberPath
ComboBox
或ListBox
答案 0 :(得分:0)
我的问题是我如何在几个课程中使用相同的模板?
简短回答:你不能。
您不能仅替换绑定路径并重新使用模板的其余部分。始终定义模板&#34;作为一个整体&#34;:
WPF: Is there a way to override part of a ControlTemplate without redefining the whole style?
所以在XAML中无法做到这一点。您可能需要考虑以编程方式创建模板:
How generate custom columns for FrameworkElementFactory(typeof(Datagrid))?