我基于
UserControl
构建了 DataGrid
,以添加我没有详细说明的过滤功能。它相当复杂,但效果很好(稍后我必须弄清楚如何删除大量隐藏代码)。
我想扩展一些样式(每个使用此
UserControl
的控件将有自己的转换器来更改行与分隔符等的显示方式),但我收到以下错误:
System.Windows.Markup.XamlParseException:“设置属性‘System.Windows.ResourceDictionary.DeferrableContent’引发异常。
InvalidOperationException:无法重新初始化 ResourceDictionary 实例。
<DataGrid x:Class="CustomDataGrid">
<DataGrid.Resources>
<!-- Data Grid Row Header -->
<Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
<!-- I would like to use the specific ControlTemplate (e.g. add row separators) defined in the control which uses this UserControl -->
</Style>
</DataGrid.Resources>
<DataGrid.Style>
<!-- DataGrid -->
<Style TargetType="{x:Type DataGrid}">
<Setter Property="RowStyle" Value="{StaticResource DataGridRowStyle}" />
</Style>
</DataGrid.Style>
</DataGrid>
<controls:CustomDataGrid>
<controls:CustomDataGrid.Resources>
<!-- Data Grid Row Header -->
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<!-- Border, SelectiveScrollingGrid and DataGridCellsPresenter included in the ControlTemplate with specific value converters, etc. -->
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</controls:CustomDataGrid.Resources>
</controls:CustomDataGrid>
抱歉,我有点不知所措。是的,一个例子总是有帮助的!
下面是另一种方法的一些细节,我认为使用
ControlTemplate
作为 DependencyProperty
“有效”(我不认为问题来自于错误的类型):
当我分析 XAML 绑定失败时,我收到错误 DataGridRowStyleControlTemplate 属性未在 VisualFieldModel 类型的对象上找到。 其中
DataContext
VisualFieldModel
(ItemsSource 是一个 ObersableCollection<VisualFieldModel>
)。
<UserControl x:class="FieldsView">
<UserControl.Resources>
<!-- Control Template -->
<ControlTemplate x:Key="FieldsDataGridRowStyleControlTemplate" TargetType="{x:Type DataGridRow}">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<SelectiveScrollingGrid>
<DataGridCellsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" />
<Path
VerticalAlignment="Top"
Data="M 0 0 L 1 0"
Stretch="Fill"
Stroke="{Binding RowStyleSeparator, Converter={StaticResource FieldRowStyleSeparatorToStrokeColorConverter}}"
StrokeDashArray="{Binding RowStyleSeparator, Converter={StaticResource FieldRowStyleSeparatorToStrokeDashArrayConverter}}"
StrokeThickness="{Binding RowStyleSeparator, Converter={StaticResource FieldRowStyleSeparatorToStrokeThicknessConverter}}" />
</SelectiveScrollingGrid>
</Border>
</ControlTemplate>
</UserControl.Resources>
<controls:CustomDataGrid
x:Name="fieldsCustomDataGrid"
DataContext="{Binding}"
DataGridRowStyleControlTemplate="{StaticResource FieldsDataGridRowStyleControlTemplate}"
ItemsSource="{Binding Fields}">
</controls:CustomDataGrid>
</UserControl>
public partial class CustomDataGrid : DataGrid
{
public static readonly DependencyProperty DataGridRowStyleControlTemplateProperty =
DependencyProperty.Register(
nameof(DataGridRowStyleControlTemplate),
typeof(ControlTemplate),
typeof(CustomDataGrid),
new PropertyMetadata(null));
public ControlTemplate DataGridRowStyleControlTemplate
{
get => GetValue(DataGridRowStyleControlTemplateProperty) as ControlTemplate;
set => SetValue(DataGridRowStyleControlTemplateProperty, value);
}
}
<DataGrid x:Class="CustomDataGrid">
<DataGrid.Resources>
<!-- Data Grid Row -->
<Style x:Key="DataGridRowStyle" TargetType="{x:Type DataGridRow}">
<!-- Basic properties which will not change -->
<Setter Property="Template" Value="{Binding DataGridRowStyleControlTemplate}" />
</Style>
</DataGrid.Resources>
<DataGrid.Style>
<!-- DataGrid -->
<Style TargetType="{x:Type DataGrid}">
<Setter Property="RowStyle" Value="{StaticResource DataGridRowStyle}" />
</DataGrid.Style>
</DataGrid>