Question: How to reference resource dictionary brushes from control resources
Given that I have a UI element like so:
<Button Style="{StaticResource ButtonRevealStyle}"><Button.Resources></Button.Resources></Button>
And I want to use a brush from a resource dictionary like this for the background:
<AcrylicBrush x:Key="CustomAcrylicBrush" ... />
Current, I am doing something like this:
<Border.Resources>
<AcrylicBrush x:Key="ButtonBackground" ... />
<AcrylicBrush x:Key="ButtonBackgroundPointerOver" ... />
<AcrylicBrush x:Key="ButtonBackgroundPressed" ... />
</Border.Resources>
However, I cannot figure out how to create a reusable brush in a resource dictionary, then use it to apply directly to a single button and override the default style without recreating the brush each time and giving it the specified key.
I know I can create templates, but given that I can do this one-off way very easily with only a few lines, and don't have to restyle the ENTIRE control just to change a few brushes, templating is overkill in this scenario.
I saw in the Generic.xaml file something like this: <StaticResource x:Key="ButtonBackground" ResourceKey="..." /> however I can't find any documentation on how this works, and it doesn't work in this case.
Is there a better way to do what I'm currently doing?
Just so I understand correctly, you want to define the resource once, and use it everywhere, right?
In that case, you can simply put the brush in the resources of App.xaml file. Every resource you define there will be available throughout all XAML files in your project. That however also means, if you set "ButtonBackground" in App.xaml, that every button will use that.
You can find an example where that is used here where a thickness was defined, that can be referenced everywhere in the project.
To clarify:
I don't want to apply these brushes to all buttons, just this specific one.
For the default background, I know I can use a staticresource reference Background={StaticResource MyCustomBrush}, however, I still need to override the PointerOver and Pressed backgrounds, which cannot be applied this way.
Oh yes, I understand your problem.
In that case, you can define your brush in App.xaml and add a StaticResource with the ResourceKey set to the name of your custom brush.
An example:
<!-- App.xaml -->
<ResourceDictionary>
<AcrylicBrush x:Key="MyBrush" > ... </AcrylicBrush>
</ResourcesDictionary>
<!-- In your page -->
....
<Button>
<Button.Resources>
<StaticResource x:Key="ButtonBackgroundPointerOver" ResourceKey="MyBrush" />
</Button.Resources>
</Button>
...
If you want to change the brush based on theme, unfortunately you need this snippet (at least that's the only thing that worked for me):
<Button>
<Button.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Dark">
<StaticResource x:Key="ButtonBackgroundPointerOver" ResourceKey="MyBrushDark"/>
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
<StaticResource x:Key="ButtonBackgroundPointerOver" ResourceKey="MyBrushLight"/>
</ResourceDictionary>
<ResourceDictionary x:Key="HighContrast">
<StaticResource x:Key="ButtonBackgroundPointerOver" ResourceKey="MyBrushHighContrast"/>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Button.Resources>
</Button>
Edit: I didn't have any luck using ThemeResource's instead of StaticResource's , changing StaticResource's to ThemeResource's always resulted in a crash.
Ok, I was able to use <StaticResource /> to apply those brushes successfully. I did notice that the hot reloading doesn't take effect, so the app has to be restarted, however, it does work after a restart. This is very helpful, thank you