欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wpf-鼠标点击/进入按钮时展示列表,移出时隐藏列表

程序员文章站 2022-06-07 15:13:37
...

要实现的效果大概是:
不点击不经过时
wpf-鼠标点击/进入按钮时展示列表,移出时隐藏列表
否则
wpf-鼠标点击/进入按钮时展示列表,移出时隐藏列表
我的思路是先做一个button,然后在button上加一个MouseEnter和Click事件。下面摆一个ListView,加一个MouseLeave事件。这些事件控制的是ListView的Visibility。数据绑定部分省略。

无边框带下划线的按钮

<Button SnapsToDevicePixels="True" Background="Transparent" Height="30" Width="150" BorderBrush="Transparent"
        MouseEnter="showHiddenFeaturesBtn_MouseEnter" 
        Click="showHiddenFeaturesBtn_Click"
        Margin="320,0,0,550" x:Name="showHiddenFeaturesBtn" Visibility="Hidden">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center">
                    <ContentPresenter.Content>
                        <TextBlock Text="没有显示的特征" FontSize="15">
                            <TextBlock.TextDecorations>
                                <TextDecorationCollection>
                                    <TextDecoration PenOffset="4"/>
                                </TextDecorationCollection>
                            </TextBlock.TextDecorations>
                        </TextBlock>
                    </ContentPresenter.Content>
                </ContentPresenter>
            </Grid>
        </ControlTemplate>
    </Button.Template>  
</Button>

ListView

<ListView x:Name="hiddenFeatureNames" Margin="320,0,0,400" Visibility="Hidden" Width="200" Height="100"
		  Background="Transparent" MouseLeave="hiddenFeatureNames_MouseLeave">
</ListView>

c#

private void showHiddenFeaturesBtn_MouseEnter(object sender, MouseEventArgs e)
{
	hiddenFeatureNames.Visibility = Visibility.Visible;
}
private void showHiddenFeaturesBtn_Click(object sender, RoutedEventArgs e)
{
	hiddenFeatureNames.Visibility = Visibility.Visible;
}
private void hiddenFeatureNames_MouseLeave(object sender, MouseEventArgs e)
{
	hiddenFeatureNames.Visibility = Visibility.Hidden;
}
相关标签: c#和WPF wpf