Reputation: 9440
I know I can set a default style for (say) all TextBox
es in my application by adding the following in App.xaml...
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="Red" />
</Style>
I would like to know how I can do this in C# instead (presumably in App.xaml.cs). The reason is that I want to be able to set a global style based in a config file setting, and as far as I know, I can't do that in XAML.
Edit Following armenm's reply, I tried using a resource dictionary. I added the XAML file...
<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>
Then used it in the App.xaml.cs startup event as follows...
ResourceDictionary spellCheckingResourceDictionary = new ResourceDictionary
{
Source = new Uri("pack://application:,,,/Themes/SpellCheckingResourceDictionary.xaml",
UriKind.RelativeOrAbsolute)
};
Current.Resources.MergedDictionaries.Add(spellCheckingResourceDictionary);
However, this didn't work. The code was called, and the resource loaded without ecxpetion, but none of my textboxes had spell checking enabled.
Anyone any ideas? Thanks.
Upvotes: 1
Views: 238
Reputation: 932
Here's the direct answer to your question - this is how this style would look like in code:
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
}
But I would recommend to do it differently: declare different sets of styles in resource dictionaries and load/unload them instead.
Here we go:
Current.Resources.MergedDictionaries.Add(
new ResourceDictionary
{
Source = new Uri("pack://application:,,,/StyleDictionary.xaml", UriKind.RelativeOrAbsolute)
});
And the style dictionary (StyleDictionary.xaml).
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Style TargetType="TextBox">
<Setter Property="SpellCheck.IsEnabled" Value="True" />
</Style>
</ResourceDictionary>
Upvotes: 2
Reputation: 2376
Maybe the real problem is your spell-checker but not the resource style.
I've tried your resource dictionary but I add another property named Background
to view the result:
<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>
I load it in the OnStartup
method:
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);
}
The background property works fine but the SpellCheck doesn't.
I find a topic talking about this: TextBox SpellCheck.IsEnabled not working in WPF 4?. As it said:
You need to install the language pack for .NET Framework 4.0 to enable spell check for some language in your WPF4 applications.
So you may have to install an en-us
language pack.
Upvotes: 2