将标签更改为Stackpanel鼠标悬停



如何使用xaml更改代码中Label和TextBlock的前景?

或者,是否可以在Stackpanel IsMouseOver事件发生后立即更改Label前景,只需使用xaml?

<Window x:Class="MouseOverTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MouseOverTest"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">

<Window.Resources>
<!--Color-->
<SolidColorBrush x:Key="BorderDesign" Color="#A73838"/>

<LinearGradientBrush x:Key="ColorDesign" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#A73838" Offset="0"/>
<GradientStop Color="#D87878" Offset="1"/>
</LinearGradientBrush>

<LinearGradientBrush x:Key="ColorMouseOverDesign" StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Color="#D87878" Offset="0"/>
<GradientStop Color="#A73838" Offset="1"/>
</LinearGradientBrush>
</Window.Resources>

<Grid>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="7*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="7*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>


<StackPanel x:Name="SP" Orientation="Vertical" Grid.Column="1" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<StackPanel.Resources>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="Background" Value="{StaticResource ColorDesign}"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource ColorMouseOverDesign}"/>
</Trigger>
</Style.Triggers>
</Style>

<Style TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>

<Style TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="White"/>
</Trigger>
</Style.Triggers>
</Style>
</StackPanel.Resources>
<Button x:Name="BT" Content="TestButton" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="50" Margin="0,10,0,5"/>
<Label x:Name="L" Content="Test Label" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" Height="50" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"/>
<TextBlock x:Name="TB" Text="This is a Test, a Test TextBlock..." VerticalAlignment="Center" HorizontalAlignment="Center" Width="200" Height="50"/>
</StackPanel>

</Grid>
</Window>

到目前为止,我找到的唯一方法就是用C#方法来改变它。但结果我有4个StackPanels 8个方法。。。

我找不到合适的方法来解决这个问题,我很感激你在这个问题上的帮助。

提前谢谢。

第一个问题:您可以在LabelTextBlock中使用Foreground属性
第二个问题:我刚刚接受了你的一种风格,并对其进行了一些修改:

<Style TargetType="Label">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Blue"/>
</Trigger>
</Style.Triggers>
</Style>  
<Label Margin="10" HorizontalAlignment="Center" Content="Your text" FontSize="20"/>

如果您现在使用Label,只要您将鼠标悬停在标签上,Foreground的颜色就会发生变化。

最新更新