Wpf 更改BasedOn控件模板内的边框背景
下面是我的应用程序资源中的XAML,它全局更改应用程序中的所有按钮控件,使其外观和行为符合我的要求:Wpf 更改BasedOn控件模板内的边框背景,wpf,xaml,controltemplate,Wpf,Xaml,Controltemplate,下面是我的应用程序资源中的XAML,它全局更改应用程序中的所有按钮控件,使其外观和行为符合我的要求: <Style TargetType="{x:Type Button}" x:Key="MyButtonStyle"> <Setter Property="SnapsToDevicePixels" Value="true"/> <Setter Property="OverridesDefaultStyle" Value="true"/>
<Style TargetType="{x:Type Button}" x:Key="MyButtonStyle">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border" CornerRadius="0" BorderThickness="0"
Background="CornflowerBlue" BorderBrush="CornflowerBlue">
<ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" />
</Border>
<ControlTemplate.Triggers>
<!-- a bunch o' triggers here -->
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
在我的应用程序的一个用户控件上,我想更改此按钮的一些属性。下面是我现在在UserControl.Resources部分中使用的一些XAML:
<Style x:Key="SpecialButton" TargetType="Button" BasedOn="{StaticResource MyButtonStyle}">
<Setter Property="Width" Value="20" />
<Setter Property="Visibility" Value="Collapsed" />
<Setter Property="Content" Value=">" />
<Setter Property="Border" Value="#eeeeee" />
<Setter Property="Border.Background" Value="#eeeeee" />
</Style>
在我的UserControl上,我为SpecialButton指定样式的Button控件具有正确的宽度、可见性和内容,但最后两次尝试无效。在这个特殊的按钮样式中,如何从应用程序资源中更改名为“Border”的边框的背景色?您可以使用
TemplateBinding
在基本样式中设置控件的background属性。同样在基础样式中,将背景颜色设置为默认的“CornflowerBlue”
谢谢你的回答,这并不完全正确,但它引导我走上了正确的道路。我最初的问题在第一个XAML代码片段中的Style元素上有一个x:Key
,这不在我的应用程序中的XAML中。因此,我必须在第二个XAML代码片段中使用BasedOn=“{StaticResource{x:Type Button}}”
,以使其尊重对原始样式的更改。
<Setter Property="Background" Value="CornflowerBlue" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="Border" Background="{TemplateBinding Background}"
<Style x:Key="SpecialButton" TargetType="Button" BasedOn="{StaticResource MyButtonStyle}">
<Setter Property="Background" Value="#eeeeee" />