问题
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.
回答1:
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>
the trigger-landscape in Wpf can be confusing sometimes, dont worry you will learn to live with it!
来源:https://stackoverflow.com/questions/55062407/wpf-hide-element-when-hovering-a-mouse-over-another-element