在 WPF XAML 中禁用样式? [英] Disable Style in WPF XAML?
问题描述
有没有办法以编程方式关闭样式?
Is there anyway to turn off a style programatically?
例如,我有一个链接到所有文本框的样式
As an example, I have a style that is linked to all textboxes
<Style TargetType="{x:Type TextBox}">
我想添加一些代码来实际停止正在使用的样式元素,因此基本上恢复到默认控件样式.
I would like to add some code to actually stop the style elements being used, so basically reverting back to the default control style.
我需要一种方法来切换我的样式,以便我可以通过 C# 代码在 Windows 默认样式和我的自定义样式之间切换.
I need a way to make a switch in my styles, so I can switch between Windows default style and my custom style through C# code.
有没有办法做到这一点?
Is there anyway to do this?
谢谢
工作解决方案
推荐答案
要将样式设置为默认,
在 XAMl 使用中,
In XAMl use,
<TextBox Style="{x:Null}" />
在 C# 中使用,
myTextBox.Style = null;
<小时>
如果需要将多个资源的样式设置为 null,请参阅 CodeNaked 的 响应.
我觉得,所有附加信息都应该在您的问题中,而不是在评论中.无论如何,在代码隐藏中,我认为这就是您要实现的目标:
I feel, all the additional info should be in your question and not in the comments. Anyways, In code Behind I think this is what you are trying to achieve:
Style myStyle = (Style)Application.Current.Resources["myStyleName"];
public void SetDefaultStyle()
{
if(Application.Current.Resources.Contains(typeof(TextBox)))
Application.Current.Resources.Remove(typeof(TextBox));
Application.Current.Resources.Add(typeof(TextBox),
new Style() { TargetType = typeof(TextBox) });
}
public void SetCustomStyle()
{
if (Application.Current.Resources.Contains(typeof(TextBox)))
Application.Current.Resources.Remove(typeof(TextBox));
Application.Current.Resources.Add(typeof(TextBox),
myStyle);
}
这篇关于在 WPF XAML 中禁用样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!