I have a styles.xaml file that lists a set of colors. These colors define how certain elements within one part of the application are shown, and thus are used through a converter.
I would like to create a legend of these colors in another part of the application, and have a toggle button list that I'd like to set the background colors to the colors defined in the styles.xaml.
Would I need to somehow include the styles.xaml file into the xaml file defining the toggle buttons? Or is there some way I can bind directly to these color values?
You can use a style on any element that derives from FrameworkElement or FrameworkContentElement such as a Window or a Button. The most common way to declare a style is as a resource in the Resources section in a XAML file.
The only concept for which there really is no correspondent in WPF is CSS class. This can easily be introduced via an attached property.
Add styles.xaml to App.xaml
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries >
<ResourceDictionary Source="styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Note Attribution for the following content / answer should go to @Chris Schaller. This answer's content was originally posted as an edit to @chameleon86 answer and was rejected (see also this meta). However I think, this is some valuable content and so I am 'reposting' it.
To make the definitions in styles.xaml available to all XAML within the app, add styles.xaml to App.xaml
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries >
<ResourceDictionary Source="styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- You can declare additional resources before or after Merged dictionaries, but not both -->
<SolidColorBrush x:Key="DefaultBackgroundColorBrush" Color="Cornsilk" />
<Style x:Key="DefaultBackgroundColor" TargetType="TextBox">
<Setter Property="Background" Value="{StaticResource DefaultBackgroundColorBrush}" />
</Style>
</ResourceDictionary>
</Application.Resources>
To understand how this works, at runtime your window, page or control will exist as child elements of the running application visual tree.
Your initial question noted:
"These colors define how certain elements within one part of the application..."
If you only need these style resources available to some xaml pages or windows, and not all of them, then you can still use this pattern to merge the local resources for a window, or for grids or other controls directly.
See how simple it is scope the style reference to a single grid for usage:
<Grid>
<Grid.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries >
<ResourceDictionary Source="styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- You can declare additional resources before or after Merged dictionaries, but not both -->
</ResourceDictionary>
</Grid.Resources>
<!--
Grid Content :)
-->
</Grid>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With