如何将自定义样式应用于自定义控件模板内的控件?

时间:2014-02-14 10:42:53

标签: c# wpf xaml custom-controls

我的模板中有一个button的自定义控件。模板位于my.xaml中引用的themes\generic.xaml文件中。

在我的应用中,我使用两种按钮样式:宽(默认,无键)和窄(x:Key="narrowButton")。它们在myDic.xaml文件中定义。

我希望模板中的按钮变窄。

但这不起作用:

<ResourceDictionary ...>
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/assembly;component/myDic.xaml" />
    <ResourceDictionary>
        <Style TargetType="{x:Type localui:MyControl}">
        <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type localui:MyControl}">
                <Button Style="{StaticResource narrowButton}">...</Button>
            </ControlTemplate>
        </Setter.Value>
        </Setter>
        </Style>
    </ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

事实上,MergedDictionaries中的my.xaml会导致该按钮完全无法显示。

narrowButton样式应用于我的按钮是什么工作方法?

1 个答案:

答案 0 :(得分:1)

检查以下代码......

<ResourceDictionary ...>
   <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="/assembly;component/myDic.xaml" />
   </ResourceDictionary.MergedDictionaries>
         <Style TargetType="{x:Type localui:MyControl}">
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate TargetType="{x:Type localui:MyControl}">
                            <Button Style="{StaticResource narrowButton}">...</Button>
                      </ControlTemplate>
                  </Setter.Value>
             </Setter>
          </Style>
</ResourceDictionary>