【发布时间】:2018-05-23 15:30:03
【问题描述】:
我知道我可以通过在 App.xaml 中添加以下内容来为(比如说)我的应用程序中的所有 TextBoxes 设置默认样式...
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="Red" />
</Style>
我想知道如何在 C# 中执行此操作(大概在 App.xaml.cs 中)。原因是我希望能够根据配置文件设置设置全局样式,据我所知,我无法在 XAML 中做到这一点。
编辑 在 armenm 的回复之后,我尝试使用资源字典。我添加了 XAML 文件...
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation">
<Style TargetType="TextBox">
<Setter Property="SpellCheck.IsEnabled"
Value="True" />
</Style>
</ResourceDictionary>
然后在App.xaml.cs启动事件中使用如下...
ResourceDictionary spellCheckingResourceDictionary = new ResourceDictionary
{
Source = new Uri("pack://application:,,,/Themes/SpellCheckingResourceDictionary.xaml",
UriKind.RelativeOrAbsolute)
};
Current.Resources.MergedDictionaries.Add(spellCheckingResourceDictionary);
但是,这不起作用。代码被调用,资源加载时没有 ecxpetion,但我的文本框都没有启用拼写检查。
有人有什么想法吗?谢谢。
【问题讨论】:
-
用你想要的初始值创建一个属性(在本例中为红色),然后绑定到它。