在自定义controltemplate上使用setter

时间:2014-05-29 07:29:33

标签: c# wpf xaml

我有一个资源字典文件,其中我有一个自定义的ControlTemplate定义

<ControlTemplate x:Key="ComboBoxToggleButton"
             TargetType="{x:Type ToggleButton}">
    <Grid>
   ....
   </Grid>
</ControlTemplate>

(取自此处:http://msdn.microsoft.com/en-us/library/ms752094(v=vs.110).aspx(字典文件文件包含整个内容))

我用它来改变控件的外观。但现在,我希望能够使用setter动态更改它的宽度。

但问题是,如果我这样做(在另一个res。字典文件中)

<Style TargetType="ComboBox">               
    <Setter Property="Width" Value="250"/>
</Style>

它完全覆盖我的自定义ControlTemplate,它使用默认的系统组合框,它应用该宽度。 但我希望该setter将宽度应用于我的自定义ControlTemplate。

1 个答案:

答案 0 :(得分:0)

当您在资源中编写样式时,它将始终覆盖默认行为
如果您希望它继承其他一些样式,您需要使用BasedOn属性:

<Style TargetType="ComboBox" BasedOn="{StaticResource ComboBoxToggleButton}">               
    <Setter Property="Width" Value="250"/>
</Style>