如何仅使用 xaml 更改代码中标签和 TextBlock 的前景?
或者甚至可以在 Stackpanel IsMouseOver 事件发生后立即更改标签前景,只需使用 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 方法......
我找不到解决此问题的正确方法,感谢您对这个问题的帮助。
谢邀。