gpt4 book ai didi

c# - 如何在我的按钮中获得一个矩形以在悬停时更改颜色?

转载 作者:太空宇宙 更新时间:2023-11-03 22:40:58 25 4
gpt4 key购买 nike

如何在我的按钮内得到一个矩形来翻转它的颜色悬停时,我似乎需要不在按钮。

这是我想要的:

Image of buttons that are needed

悬停时看到按钮底部下方的蓝色矩形我需要它从按钮颜色更改为蓝色。

我试过这个:

<Style TargetType="{x:Type Button}" x:Key="MenuButton">
<Setter Property="Background" Value="#d8d8d8" />
<Setter Property="Foreground" Value="Black" />

<Setter Property="anything" Value="Blue" />

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True">

<!-- Inner Rectangle with rounded corners. -->
<Rectangle x:Name="innerRectangle" Fill="{TemplateBinding Background}"/>
<!-- Present Content (text) of the button. -->
<DockPanel Name="myContentPresenterDockPanel" HorizontalAlignment="center">
<ContentPresenter x:Name="myContentPresenter" Margin="12" TextBlock.Foreground="{TemplateBinding Foreground}" TextBlock.FontSize="14" TextBlock.FontWeight="Light"/>
</DockPanel>
<Rectangle x:Name="test" Fill="{TemplateBinding anything }" Height="4" VerticalAlignment="Bottom"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#d2d2d2"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="anything" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>

显然,如果 fill 是按钮中的一个属性,那会起作用,我的想法是不是完全错误?

谢谢大家!

最佳答案

您将必须使用 ControlTemplate.Triggers,然后您可以使用 setter 中的 TargetName 访问您的矩形。像这样:

<Style TargetType="{x:Type Button}" x:Key="MenuButton">
<Setter Property="Background" Value="#d8d8d8" />
<Setter Property="Foreground" Value="Black" />

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True">

<!-- Inner Rectangle with rounded corners. -->
<Rectangle x:Name="innerRectangle" Fill="{TemplateBinding Background}"/>
<!-- Present Content (text) of the button. -->
<DockPanel Name="myContentPresenterDockPanel" HorizontalAlignment="center">
<ContentPresenter x:Name="myContentPresenter" Margin="12" TextBlock.Foreground="{TemplateBinding Foreground}" TextBlock.FontSize="14" TextBlock.FontWeight="Light"/>
</DockPanel>
<Rectangle x:Name="test" Fill="Black" Height="4" VerticalAlignment="Bottom"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#d2d2d2"/>
<Setter Property="Foreground" Value="White"/>
<Setter TargetName="test" Property="Fill" Value="Green" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

关于c# - 如何在我的按钮中获得一个矩形以在悬停时更改颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52202919/

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