WPF-将鼠标悬停在另一个元素上时隐藏元素 [英] WPF - hide element when hovering a mouse over another element
问题描述
网格中有一些元素,我只想在鼠标悬停在矩形上方时显示/隐藏按钮.
There are some elements in a grid, and all I want is to show/hide the button when a mouse is over a rectangle.
<UserControl ...>
<Grid>
<Rectangle ...>
<Button ...>
</Grid>
</UserControl>
我已经尝试了多个触发器,但到目前为止仍未成功.请帮忙.
I've tried several triggers but was unsuccessful so far. Please help.
推荐答案
这是您需要的:绑定到控件以触发它的数据触发器或者,您可以使用类似 BoolenToInvisibilityConverter
This is what you need:
A datatrigger bound to the Control to trigger it
alternativly you could use a converter sth like a BoolenToInvisibilityConverter
如果要反转逻辑,则需要在样式中设置可见性,否则将被覆盖
Ps if you want to invert the logic you need to set the visibility in the style as it would be overwritten otherwhise
<StackPanel>
<Rectangle Fill="Red" Height="20" Width="29" Name="MyRect"/>
<Button>
<Button.Style>
<Style TargetType="Button">
<!--<Setter Property="Visibility" Value="Hidden"/>-->
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MyRect, Path=IsMouseOver}" Value="True">
<Setter Property="Visibility" Value="Hidden" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</StackPanel>
Wpf中的触发景观有时会令人困惑,不要担心,您将学会使用它!
the trigger-landscape in Wpf can be confusing sometimes, dont worry you will learn to live with it!
这篇关于WPF-将鼠标悬停在另一个元素上时隐藏元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!