有没有办法以编程方式关闭样式?
例如,我有一个链接到所有文本框的样式
<Style TargetType="{x:Type TextBox}">
我想添加一些代码来实际停止正在使用的样式元素,因此基本上恢复到默认控件样式。
我需要一种方法来切换我的样式,这样我就可以通过 C# 代码在 Windows 默认样式和我的自定义样式之间切换。
有什么办法吗?
谢谢
工作解决方案
最佳答案
将样式设置为默认,
在 XAMl 中使用,
<TextBox Style="{x:Null}" />
在C#中使用,
myTextBox.Style = null;
如果需要为多个资源将样式设置为 null,请参阅 CodeNaked 的 响应。
我觉得,所有附加信息都应该在您的问题中,而不是在评论中。无论如何,在代码隐藏中,我认为这就是您要实现的目标:
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);
}
关于c# - 在 WPF XAML 中禁用样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6946112/