WPF Popup带箭头,自适应箭头位置在target中间的实现方式

如题,在WPF项目中,有时会涉及使用Popup控件实现一些弹出式菜单:

1.程序不靠屏幕边框时,箭头居于Popup中央,指向target中央

 

 2.程序靠边框时,Popup自动偏移,箭头仍然指向target控件的中央,这样比较美观和谐

 

并且,当target控件比较靠下时,Popup控件会自动翻转到target的上方,箭头向下

 这里使用了我自定义的一个Popup控件:CustomizePopup 

复制代码
            <!--Popup-->
            <local:CustomizePopup StaysOpen="False" AllowsTransparency="True" IsOpen="{Binding IsPopupMore, ElementName=header}" PlacementTarget="{Binding ElementName=btnMore}" Placement="Bottom">
                <StackPanel Margin="2">
                    <StackPanel.Effect>
                        <DropShadowEffect Color="Gray" Opacity="0.4" ShadowDepth="0" Direction="270" />
                    </StackPanel.Effect>
                    <Path x:Name="arrowTop" StrokeThickness="0" Fill="{DynamicResource PopupBackgroundColor}" HorizontalAlignment="Left" Data="M11,0 0,10 22,10Z"/>
                    <Border Background="#FF242424" Width="135" CornerRadius="4">
                        <StackPanel Margin="0,0">
                            <local:ImageButton Content="修改密码" Height="35" Style="{StaticResource PopupMenuBtnStyle}" CornerRadius="4,4,0,0"/>
                            <local:ImageButton Content="管理后台" Height="35" Style="{StaticResource PopupMenuBtnStyle}" Margin="0,1,0,0"/>
                            <local:ImageButton Content="退出登录" Height="35" Style="{StaticResource PopupMenuBtnStyle}" Margin="0,1,0,0" CornerRadius="0,0,4,4"/>
                        </StackPanel>
                    </Border>
                    <Path x:Name="arrowBottom" StrokeThickness="0" Fill="{DynamicResource PopupBackgroundColor}" HorizontalAlignment="Left" Data="M0,0 22,0 11,10Z"/>
                </StackPanel>
            </local:CustomizePopup>
复制代码

 

posted on   lopengye  阅读(350)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2019-07-25 asp.net mvc 设置文本框的宽高

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示