设置自定义样式后默认样式丢失(例如颜色)

Default style lost (e.g. colors) after setting custom style

我使用 telerik 中的 RadTreeView 来显示带有节点的树。要将 IsExpanded 属性 绑定到我自己的 IsExpanded 属性,我使用以下代码片段:

<Style TargetType="{x:Type telerik:RadTreeViewItem}" x:Key="ItemContainerStyle"  >
    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded,Mode=TwoWay}"/>
</Style>

目前为止一切正常,但节点的高亮颜色从蓝色变为灰色。怎样才能保持原来的风格,只加setter 属性?

编辑:

我使用 telerik Windows8Theme 并调整 Windows8Palette。 在 XAML 中添加提到的样式元素之前,所选元素的颜色是 Windows8 调色板的 AccentColor(蓝色)。添加样式元素后,它似乎使用了 Windows8 调色板的 BasicColor(灰色)。我不知道到底发生了什么,但比较 RGB 值显示了这个颜色切换。

您需要继承默认样式:

<Style x:Key="ItemContainerStyle"
       TargetType="{x:Type telerik:RadTreeViewItem}"
       BasedOn="{x:Type telerik:RadTreeViewItem}">
    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded,Mode=TwoWay}"/>
</Style>

BasedOn="{x:Type telerik:RadTreeViewItem}" 确实告诉它继承当前的默认样式,只是 "add" 您的 setter。

应该是:

<Style x:Key="ItemContainerStyle" TargetType="{x:Type telerik:RadTreeViewItem}" BasedOn="{StaticResource {x:Type telerik:RadTreeViewItem}}">
    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded,Mode=TwoWay}"/>
</Style>

...如果您希望自定义样式基于默认样式。

我终于得到了答案。我从另一个程序集中覆盖了我们的自定义样式。这在这里有效:

<telerik:radGridView.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="pack://application:,,,/OtherAssembly,component/existingStyles.xaml />
    </ResourceDictionary.MergedDictionaries>
    <Style BasedOn="{StaticResource xKeyOfStyleToExtendFromExistingStyles}" TargetType="{x:Type telerik:RadTreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}"/>
    </Style>
  </ResourceDictionary>
</telerik:radGridView.Resources>