gpt4 book ai didi

wpf - 在按钮悬停时将文本和路径更改为不同的颜色

转载 作者:行者123 更新时间:2023-12-05 09:20:16 25 4
gpt4 key购买 nike

我有一个包含路径和文本的按钮。像这样

enter image description here

在按钮悬停时它应该像这样改变颜色 enter image description here

这是我已经完成的事情Xaml文件

<Button  Grid.Row="1" Grid.Column="0" Style="{StaticResource DashBordMenuButton}" >
<StackPanel >
<Path Name="DIcon1" Data="{StaticResource IconReporting}" Fill="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" HorizontalAlignment="Center" ></Path>
<TextBlock HorizontalAlignment="Center" Margin="0,10,0,0" > DASHBORD</TextBlock>
<TextBlock LineHeight="25" TextWrapping="Wrap" LineStackingStrategy="BlockLineHeight" Margin="0,20,0,0" Text="Minir All Servers,Clients and Activies"></TextBlock>
</StackPanel>
</Button>

风格

<Style x:Key="DashBordMenuButton" TargetType="Button">

<Setter Property="Margin" Value="10"></Setter>
<Setter Property="Background" Value="White"/>
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border" BorderThickness="1,1,1,4" BorderBrush="Gray" Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FF36D8E0"/>
<Setter Property="Foreground" Value="White"></Setter>
</Trigger>

</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>


</Style>

在悬停时它的工作正常我想要的。但默认路径图标也变成黑色而不是白色。我该如何解决这个问题提前致谢

最佳答案

您的路径由下面的 XAML 填充按钮 Forground 颜色

 Fill="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}"

因此,在您的 style 触发 Hover 之前,您的 ButtonForground 默认为黑色。您可以通过在触发器启动之前添加一个 setter 来更改它。为 ButtonForground 添加一个 setter,如下所示。

<Style x:Key="DashBordMenuButton" TargetType="Button">
<Setter Property="Margin" Value="10"></Setter>
<Setter Property="Background" Value="White"/>
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Foreground" Value="#FF36D8E0"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">

<Border Name="border" BorderThickness="1,1,1,4" BorderBrush="Gray" Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FF36D8E0"/>
<Setter Property="Foreground" Value="White"></Setter>
</Trigger>

</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

style 添加了一个 setter

 <Setter Property="Foreground" Value="#FF36D8E0"></Setter>

关于wpf - 在按钮悬停时将文本和路径更改为不同的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38355126/

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