【发布时间】:2023-03-16 16:44:01
【问题描述】:
我正在尝试设计一个按钮模板,当按下时它会交换颜色。
不幸的是,我无法让它工作。
我使用 BorderBrush 作为临时变量。很可能有更复杂的解决方案。
这是我的代码。
<Style TargetType="{x:Type Button}">
<Style.Setters>
<Setter Property="Foreground"
Value="{StaticResource RahmenFarbe}" />
<Setter Property="Background"
Value="{StaticResource HintergrundFarbe}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
ClipToBounds="True">
<Border BorderBrush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Foreground}"
x:Name="ButtonBorder"
CornerRadius="25"
BorderThickness="4"
Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Background}"
RenderTransformOrigin="0.5,0.5">
<TextBlock x:Name="ButtonTextBlock"
Foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button, AncestorLevel=1}, Path=Foreground}"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<ContentPresenter x:Name="myContentPresenter"
Content="{TemplateBinding Content}" />
</TextBlock>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed"
Value="True">
<Setter Property="BorderBrush"
Value="{Binding Mode=OneTime, Path=Foreground}" />
<Setter Property="Foreground"
Value="{Binding Mode=OneTime, Path=Background}" />
<Setter Property="Background"
Value="{Binding Mode=OneTime, Path=BorderBrush}" />
</Trigger>
</ControlTemplate.Triggers >
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
任何帮助将不胜感激。
问候塞巴斯蒂安
【问题讨论】:
标签: wpf templates xaml triggers styles