【问题标题】:How do I set a default style in C# code (not in App.xaml)?如何在 C# 代码中(不在 App.xaml 中)设置默认样式?
【发布时间】: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,但我的文本框都没有启用拼写检查。

有人有什么想法吗?谢谢。

【问题讨论】:

  • 用你想要的初始值创建一个属性(在本例中为红色),然后绑定到它。

标签: c# wpf xaml


【解决方案1】:

也许真正的问题是您的拼写检查器而不是资源样式。


我已经尝试过您的资源字典,但我添加了另一个名为 Background 的属性来查看结果:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Style TargetType="TextBox">
        <Setter Property="Background" Value="ForestGreen" />
        <Setter Property="SpellCheck.IsEnabled" Value="True" />
    </Style>
</ResourceDictionary>

我在OnStartup方法中加载它:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    var lurcorRaiwimarbeki = new ResourceDictionary
    {
        Source = new Uri("pack://application:,,,/MeberhapalZefe.xaml", UriKind.RelativeOrAbsolute)
    };
    Current.Resources.MergedDictionaries.Add(lurcorRaiwimarbeki);
}

background 属性可以正常工作,但 SpellCheck 不能。


我发现一个话题在谈论这个:TextBox SpellCheck.IsEnabled not working in WPF 4?。正如它所说:

您需要安装 .NET Framework 4.0 的语言包才能在 WPF4 应用程序中启用某些语言的拼写检查。

因此您可能需要安装en-us 语言包。

【讨论】:

  • 谢谢,但问题是资源字典。我尝试使用简单的前景样式,这在字典中也不起作用,但在 App.xaml 中起作用。不知道你为什么认为我需要一个en-US 语言包。默认情况下,拼写检查使用机器的默认语言。因为我在英国,所以我一开始就不想要美国语言包!
【解决方案2】:

这是您问题的直接答案 - 这就是这种风格在代码中的样子:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    var style = new Style();
    style.Setters.Add(new Setter(TextBox.ForegroundProperty, Brushes.Red));
    Application.Current.Resources.Add(typeof(TextBox), style);
}

void SomeOtherFunctionCalledLater()
{
    Application.Current.Resources.Remove(typeof(TextBox));

    // create another style, maybe
}

但我建议以不同的方式进行:在资源字典中声明不同的样式集,然后加载/卸载它们。

我们开始吧:

Current.Resources.MergedDictionaries.Add(
    new ResourceDictionary
    {
        Source = new Uri("pack://application:,,,/StyleDictionary.xaml", UriKind.RelativeOrAbsolute)
    });

还有样式字典(StyleDictionary.xaml)。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <Style TargetType="TextBox">
        <Setter Property="SpellCheck.IsEnabled" Value="True" />
    </Style>
</ResourceDictionary>

【讨论】:

  • 谢谢。我尝试使用资源字典,但无法正常工作。请查看我更新的问题,看看您是否可以提供帮助。再次感谢。
  • 奇怪,我刚试过,它工作。我也会将其添加到我的答案中。
  • 这里发生了一些奇怪的事情。我去掉了拼写检查样式,改为添加了一个简单的Foreground="red",它也不起作用,所以它与拼写检查无关,与设置样式有关。直接在 App.xaml 中使用相同的样式可以正常工作。有任何想法吗?再次感谢
  • 一个可以重现问题的最小项目会有所帮助。我猜项目结构有问题。这与特定属性无关 - 都必须工作。而且我无法想象这是任何版本的 WPF 中的错误 - 这是一个非常基本的功能,必须在任何地方都可以使用。
猜你喜欢
  • 1970-01-01
  • 2019-10-29
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
相关资源
最近更新 更多