WPF - hide element when hovering a mouse over another element

坚强是说给别人听的谎言 提交于 2021-01-28 01:31:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!