Reputation: 6941
I don't think this has been asked before, if so please redirect me. I'm new to WPF, and I've tried everything I could think of with no success, I'm stuck.
I'm using a WPF Theme, and I want to add some custom styles I created to it. For example, all TextBlock
are supposed to have Red foreground, but I have a group of TextBlock
that I want in Blue.
So far I've been doing this in the xaml, creating a <Style></Style>
in the resources, and calling it using Style="{StaticResource StyleName}"
. But I want to add it to the theme files instead, and I don't know how to give it a name and call it from the xaml.
I guess I'd start with something like this, but how do I link both elements?
In the theme file (MyStyles.xaml or TextEdit.xaml or similar):
<Style TargetType="{x:Type TextBlock}" x:Key="KeyName" ???>
<Setter Property="Foreground" Value="Blue" />
</Style>
And then in my xaml:
<TextBlock Name="TextBlockName"
Style="{???}">
</TextBlock>
I need this style to be in the Theme because the program will allow users to change themes, and these styles can't hardcoded be in the xaml.
Upvotes: 0
Views: 888
Reputation: 1098
You want to first merge that resource file into your resources :
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MyStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<dxc:IntToBooleanConverter x:Key="IntToBooleanConverter" />
(...)
</ResourceDictionary>
</UserControl.Resources>
and then you can use it with
<TextBlock Name="TextBlockName" Style="{StaticResource KeyName}" />
Upvotes: 1
Reputation: 43636
If you have loaded your Theme file you can access any of the Styles/Resources the same way as local Styles/Resources
If you use Style="{StaticResource StyleName}"
it will look first in the Window/UserControl, if not found it will look though the loaded Resource dictionaries. so as long as you have loaded the Theme (Resource Dictionary) it will work fine.
Upvotes: 1