【问题标题】:Default style lost (e.g. colors) after setting custom style设置自定义样式后默认样式丢失(例如颜色)
【发布时间】:2017-08-11 05:53:15
【问题描述】:

我使用来自telerik 的RadTreeView 来显示带有节点的树。要将 IsExpanded 属性绑定到我自己的 IsExpanded 属性,我使用以下 sn-p:

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

到目前为止,这工作正常,但节点的突出显示颜色从蓝色变为灰色。如何保持原有样式,只添加setter属性?

编辑:

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

【问题讨论】:

  • 这听起来像是telerik 以某种方式将IsExpanded 属性连接到高亮颜色。您是否尝试过通过混合研究风格?

标签: c# wpf xaml telerik treeview


【解决方案1】:

你需要继承默认的样式:

<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}" 确实告诉它继承当前的默认样式并只是“添加”您的设置器。

【讨论】:

  • 感谢您的快速回答。当我添加此属性时,它说:“无法分配指定的值。预期以下类型:“样式””。
  • 到目前为止我发现它应该是: BasedOn={StaticResource ResourceKey={x:type telerik:RadTreeViewItem}} 这继承自telerik默认样式但覆盖了自定义主题。
【解决方案2】:

应该是:

<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 默认样式。但我想使用在添加 Style 元素之前使用的相同样式。那是一种自定义样式,而不是默认的 Telerik 样式。
  • 那么你应该基于这个自定义样式的样式:BasedOn="{StaticResource TheXKeyOfTheCustomStyle}">
  • 我想我没有静态资源来作为我的风格的基础。问题是我们使用的 Telerik Windows8Theme 在使用 XAML 样式元素时不再正确应用于树。由于某种原因,在选择节点时使用了 Windows8Palette 的 BasicColor,而不是 AccentColor。另请参阅我在开场问题中的编辑。
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多