gpt4 book ai didi

c# - 通过基于样式属性覆盖样式

转载 作者:行者123 更新时间:2023-11-30 15:30:07 24 4
gpt4 key购买 nike

我有一个按钮的基本样式:

<Style x:Key="ButtonStyle_base" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Name="grid" Margin="0,0,0,0">
<Rectangle Name="rectangle" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Rectangle.Effect>
<DropShadowEffect BlurRadius="3" Opacity="0.4" ShadowDepth="6"/>
</Rectangle.Effect>
</Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True" />
<Trigger Property="IsDefaulted" Value="True" />
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

所以,在网格中我有一个矩形。为了让我的应用程序可换肤,我在另一个资源字典中放入了一个基于此的样式:

<Style x:Key="ButtonStyle" BasedOn="{StaticResource ButtonStyle_base}">
<Setter Property="Rectangle.Fill" >
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
<GradientStop Color="#FFFFFFFF" Offset="0.5" />
<GradientStop Color="#CCFFFFDD" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>

我想要的是改变矩形的填充。它不起作用。我怎样才能做到这一点?

最佳答案

Button的Fill with Background属性绑定(bind),

<Style x:Key="ButtonStyle_base" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Name="grid" Margin="0,0,0,0">
<Rectangle Name="rectangle" HorizontalAlignment="Stretch" Fill="{TemplateBinding Background}" VerticalAlignment="Stretch" >
<Rectangle.Effect>
<DropShadowEffect BlurRadius="3" Opacity="0.4" ShadowDepth="6"/>
</Rectangle.Effect>
</Rectangle>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True" />
<Trigger Property="IsDefaulted" Value="True" />
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

从您派生的样式设置背景,

<Style x:Key="ButtonStyle" BasedOn="{StaticResource ButtonStyle_base}">
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1" >
<GradientStop Color="#FFFFFFFF" Offset="0.5" />
<GradientStop Color="#CCFFFFDD" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>

关于c# - 通过基于样式属性覆盖样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22661435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com