Skip to content

Commit 5615a89

Browse files
Merge pull request #1942 from syncfusion-content/Update_SkinManager_UG_Hotfix
Updated the SfSkinManager UG related to TryFindResource
2 parents d238e01 + bcebc64 commit 5615a89

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

wpf/Themes/Skin-Manager.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,75 @@ Dark Theme:
536536

537537
![Customizing using ThemeResource Dark](Skin-Manager_images/Customizing-using-ThemeResource-Dark.png)
538538

539+
### Accessing Theme Resources Using TryFindResource
540+
541+
To access specific theme resources in your WPF application, you can use the `TryFindResource(key)` method to retrieve a resource by its key and apply it in the code-behind.
542+
543+
Theme resource keys are typically defined in:
544+
545+
1) **Brushes.xaml:** Contains brush-related keys such as ContentBackground, ContentForeground, etc.
546+
547+
2) **Common.xaml:** Contains font size and style keys like HeaderTextStyle, SubHeaderTextStyle, etc.
548+
549+
These files are located under the Common folder when [exporting a theme from ThemeStudio](https://help.syncfusion.com/wpf/themes/theme-studio#exporting-theme-project). You can also refer to the [Resource Key list](https://help.syncfusion.com/wpf/themes/skin-manager#resource-key-list) to locate specific styles for various controls.
550+
551+
**Example: Applying Theme Resources to a Button**
552+
553+
{% tabs %}
554+
555+
{% highlight XAML %}
556+
557+
<Button x:Name="myButton" Content="Submit" Height="30" Width="120" Margin="10"></Button>
558+
559+
{% endhighlight %}
560+
561+
{% endtabs %}
562+
563+
Include the following namespace and apply the implementation in your code-behind to retrieve and assign theme-specific resources for a button control.
564+
565+
{% tabs %}
566+
567+
{% highlight C# %}
568+
569+
using Syncfusion.Themes.Windows11Light.WPF;
570+
571+
Theme currentTheme = SfSkinManager.GetTheme(myButton);
572+
573+
if (currentTheme.ThemeName == "Windows11Light")
574+
{
575+
var buttonStyleKey = new ThemeKey
576+
{
577+
Theme = typeof(Windows11LightSkinHelper),
578+
Key = "WPFPrimaryButtonStyle"
579+
};
580+
var textStyleKey = new ThemeKey
581+
{
582+
Theme = typeof(Windows11LightSkinHelper),
583+
Key = "HeaderTextStyle"
584+
};
585+
var foregroundStyleKey = new ThemeKey
586+
{
587+
Theme = typeof(Windows11LightSkinHelper),
588+
Key = "ContentForeground"
589+
};
590+
Style buttonStyle = TryFindResource(buttonStyleKey) as Style;
591+
myButton.Style = buttonStyle;
592+
double fontSize = (double)TryFindResource(textStyleKey);
593+
myButton.FontSize = fontSize;
594+
SolidColorBrush backgroundBrush = TryFindResource(foregroundStyleKey) as SolidColorBrush;
595+
myButton.Foreground = backgroundBrush;
596+
}
597+
598+
{% endhighlight %}
599+
600+
{% endtabs %}
601+
602+
This example demonstrates how to dynamically apply a button style, foreground color, and font size using theme resource keys defined in the Brushes.xaml and Common.xaml files for the Windows11Light theme.
603+
604+
**Output Screenshot**
605+
606+
![Accessing Theme Resources Using TryFindResource](Skin-Manager_images/Accessing-Theme-Resources-Using-TryFindResource.png)
607+
539608
## Resource Key List
540609

541610
### Framework Controls
3.65 KB
Loading

0 commit comments

Comments
 (0)