添加 x:key 时样式 BasedOn 中断

Style BasedOn breaks when x:key is added

我有用户控件,它为 telerik 的控件定义自定义样式 RadTreeViewItem。问题是当我没有 x:key 时没有任何错误但是当我添加 x:key 属性 时我有错误“The resource "RadTreeViewItemStyle" could not be resolved.

        <ResourceDictionary>
            <Style x:Key="MySuperGoodStyle" TargetType="telerik:RadTreeViewItem" BasedOn="{StaticResource RadTreeViewItemStyle}">
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            </Style>
        </ResourceDictionary>

在运行时没有任何问题,样式按预期工作正常,因此看起来只是设计错误。

尝试在之前添加:

  <Style TargetType="telerik:RadTreeViewItem" x:Key="RadTreeViewItemStyle">
        <Setter Property="FocusVisualStyle" Value="{StaticResource TreeviewFocusVisual}"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Padding" Value="1 4 5 4"/>
        <Setter Property="MinHeight" Value="24"/>
        <Setter Property="IsDropAllowed" Value="True"/>
        <Setter Property="IsEnabled" Value="True"/>
        <Setter Property="IsTabStop" Value="False"/>
        <Setter Property="telerik:DragDropManager.TouchDragTrigger" Value="TapAndHold"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="Local"/>
        <Setter Property="Template" Value="{StaticResource TreeViewItemDefaultTemplate}"/>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <telerik:TreeViewPanel VerticalAlignment="Bottom"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>

并确保在您的 header 中包含:

         xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"