Is there any way to affect xaml Icons with style?

333 views Asked by At

I've included xaml icons in my WPF application using resource dictionaries and I'm using ContentControl to render the resource icon on the window.

<MenuItem.Header>
       <StackPanel Orientation="Horizontal">
              <ContentControl Content="{StaticResource appbar_database}" />
       </StackPanel>
</MenuItem.Header>

Now, How can I apply style to the icon inside the ContentControl? e.g. Setting width to 24px.

EDIT 1

This is how my resource dictionary looks like:

<ControlTemplate x:Key="appbar_database">
    <Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="76" Height="76" Clip="F1 M 0,0L 76,0L 76,76L 0,76L 0,0">
        <Path Width="34" Height="38" Canvas.Left="21" Canvas.Top="19" Stretch="Fill" Fill="#FF000000" Data="F1 M 38,19C 47.3888,19 55,21.0147 55,23.5038L 55,25.5C 55,27.9853 47.3888,30 38,30C 28.6112,30 21,27.9853 21,25.5L 21,23.5C 21,21.0147 28.6112,19 38,19 Z M 55,52.5C 55,54.9853 47.3888,57 38,57C 28.6112,57 21,54.9853 21,52.5L 21,46.5C 21,48.9853 28.6112,51 38,51C 47.384,51 54.9921,48.9874 55,46.5039L 55,52.5 Z M 55,43.5C 55,45.9853 47.3888,48 38,48C 28.6112,48 21,45.9853 21,43.5L 21,37.5C 21,39.9853 28.6112,42 38,42C 47.384,42 54.9921,39.9874 55,37.5038L 55,43.5 Z M 55,34.5C 55,36.9853 47.3888,39 38,39C 28.6112,39 21,36.9853 21,34.5L 21,28.5C 21,30.9853 28.6112,33 38,33C 47.384,33 54.9921,30.9874 55,28.5038L 55,34.5 Z "/>
    </Canvas>
</ControlTemplate>
1

There are 1 answers

2
Chris W. On BEST ANSWER

Check out a previous related answer here. The only difference is you're going to bind those properties to the template so you can effect them at the instance.

So for example, if you add a;

<Setter Property="Width" Value="34"/>

to the template so you establish a default for that property, and then on your Path INSIDE the template change it to;

Width="{TemplateBinding Width}"

Then...at the instance level you can put;

<ContentControl Style="{StaticResource YourStyleTemplateForThisThing}" Width="24"/>

Same applies to most properties, hope this helps. Cheers.