gpt4 book ai didi

c# - ControlTemplate.Triggers 中更灵活的 setter

转载 作者:太空狗 更新时间:2023-10-30 00:31:17 24 4
gpt4 key购买 nike

我正在尝试在 XAML 中设置按钮的样式。您可以在下面看到我到目前为止创建的内容。

<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#f2f2f7"/>
<Setter Property="Padding" Value="6,4"/>
<Setter Property="Foreground" Value="#222222" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="1" Background="{TemplateBinding Background}" BorderThickness="1" BorderBrush="#b1b1c0">
<Border CornerRadius="1" Background="{TemplateBinding Background}" BorderThickness="1,1,1,0" BorderBrush="#f8f8fa" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center">
</ContentPresenter>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#8e8eba" />
<Setter Property="Foreground" Value="#f2f291" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

问题是,这个按钮有两个 <Border>元素相互嵌套。我想要不同的 BorderBrush="" IsMouseOver 时的属性触发器被激活。例如,当我将鼠标放在按钮上时,内边框为红色,外边框为蓝色。

你能帮我吗?

最佳答案

尝试为边框设置名称并使用 TargetName在触发器中是这样的:

<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="#f2f2f7"/>
<Setter Property="Padding" Value="6,4"/>
<Setter Property="Foreground" Value="#222222" />

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="OuterBorder" CornerRadius="1" Background="{TemplateBinding Background}" BorderThickness="1" BorderBrush="#b1b1c0">
<Border Name="InnerBorder" CornerRadius="1" Background="{TemplateBinding Background}" BorderThickness="1,1,1,0" BorderBrush="#f8f8fa" Padding="{TemplateBinding Padding}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</Border>

<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#8e8eba" />
<Setter Property="Foreground" Value="#f2f291" />

<!-- Here use TargetName -->
<Setter TargetName="OuterBorder" Property="BorderBrush" Value="Red" />
<Setter TargetName="InnerBorder" Property="BorderBrush" Value="Blue" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

Some notes

  • TargetName 只能设置在<ControlTemplate.Triggers> 中, 不在 <Style.Triggers>
  • TargetName 仅在 ControlTemplate 内运行节

关于c# - ControlTemplate.Triggers 中更灵活的 setter ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27942774/

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