添加x:key时,基于样式的样式会中断

时间:2019-03-27 10:18:59

标签: c# wpf xaml telerik

我有用户控件,该控件定义了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>

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

1 个答案:

答案 0 :(得分:1)

尝试在此之前添加:

  <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>

并确保标题中包含

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