【发布时间】:2010-01-26 07:23:35
【问题描述】:
如何告诉控件或控件块不继承任何样式并恢复为默认值?
我需要这个,因为我有一个总体主题很有效,但其中一件真的搞砸了我的设计。
【问题讨论】:
如何告诉控件或控件块不继承任何样式并恢复为默认值?
我需要这个,因为我有一个总体主题很有效,但其中一件真的搞砸了我的设计。
【问题讨论】:
您可以为有问题的 UI 创建自己的容器,并将 InheritanceBehavior 设置为 SkipAllNow(或适合您情况的值):
using System.Windows;
using System.Windows.Controls;
public class NoInheritanceContentControl : ContentControl
{
public NoInheritanceContentControl()
{
InheritanceBehavior = InheritanceBehavior.SkipAllNow;
}
}
然后您可以将NoInheritanceContentControl 粘贴到您的可视化树中,其中的任何内容都不会继承样式。
【讨论】:
BasedOn="{StaticResource {x:Type ...}}" 提供样式。我修了两天,没有运气。 :/ 如果我遗漏了什么,请告诉我。
这很棘手,但你可以试试这个,
创建要保留默认样式的控件的命名样式,如下所示..
<Style
x:Key="DefaultButtonStyle"
TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Type Button}}"/>
确保在应用任何主题之前添加此 DefaultStyles,它应该是应用资源中的第一个条目。
然后您可以在运行时将样式“DefaultButtonStyle”应用于任何控件以保留其默认样式。
【讨论】: