我想在 mouseEnter 事件触发时更改元素的背景颜色。如何让背景颜色变深??我想我可以使用不透明蒙版,但它是渐变的,但我需要它是坚实的。它也必须在 visual basic 代码中,而不是在 xaml 中。 请帮助我!
最佳答案
不透明蒙版不是一个很好的选择,因为它会修改不透明度。此外,不透明 mask 可以是任何类型的画笔,不必是渐变。
您可以执行以下两项操作之一:操纵当前画笔或在控件顶部添加一个黑色矩形并更改矩形的不透明度。
如果您让我知道您喜欢什么,我可以编写一些代码。
(为什么一定要是代码而不是xaml?)
编辑
<Window x:Class="TestWpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfApplication"
Title="MainWindow"
Height="350"
Width="525">
<StackPanel>
<Grid>
<TextBox Background="Red"
FontSize="24" />
<Rectangle x:Name="overlay"
Fill="Black"
IsHitTestVisible="False"
Opacity="0" />
<Grid.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="0.9"
Duration="0:0:0.2"
Storyboard.TargetName="overlay"
Storyboard.TargetProperty="(Rectangle.Opacity)" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation To="0"
Duration="0:0:0.2"
Storyboard.TargetName="overlay"
Storyboard.TargetProperty="(Rectangle.Opacity)" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
</Grid>
</StackPanel>
</Window>
关于wpf - 加深元素颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5929265/