有没有办法影响带风格的xaml图标?

时间:2015-06-15 18:47:13

标签: c# wpf xaml resourcedictionary wpf-style

我在使用资源字典的WPF应用程序中包含了xaml图标,我正在使用ContentControl在窗口上呈现资源图标。

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

现在,我如何将样式应用于ContentControl内的图标?例如将宽度设置为24px。

编辑1

这是我的资源字典的样子:

<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 个答案:

答案 0 :(得分:1)

查看之前的相关回答here。唯一的区别是您要将这些属性绑定到模板,以便您可以在实例中实现它们。

例如,如果添加;

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

到模板,以便为该属性建立默认值,然后在路径INSIDE上将模板更改为;

Width="{TemplateBinding Width}"

然后......在你可以放置的实例级别上;

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

同样适用于大多数房产,希望这会有所帮助。欢呼声。