C# wpf 解决Popup弹出位置异常问题

问题描述

使用Popup控件作为弹出框,使用相对位置弹出即Placement=“Relative”,在不同的设备中弹出的位置不一致。比如下面的例子。
使用如下代码:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="360" Width="640">
    <Grid>
        <ToggleButton x:Name="Btn_Popup"  Width="70" Height="35"  Content="弹出"  />
        <Popup  x:Name="Popup1" 
                            Placement="Relative"
                            AllowsTransparency="True"
                            PlacementTarget="{Binding ElementName=Btn_Popup}" 
                            IsOpen="{Binding IsChecked, ElementName=Btn_Popup}"
                            StaysOpen="False"
                            Width="120" 
                            Height="120"
                            HorizontalOffset="80" 
                            VerticalOffset="-125"                        
                            >
            <Grid>
                <Border   Width="120" Height="60" BorderBrush="#333333" BorderThickness="1" CornerRadius="10" Background="White" >
                    <TextBlock Text="弹出框"  FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
            </Grid>
        </Popup>
    </Grid>
</Window>

显示效果:
在这里插入图片描述
在这里插入图片描述

原因分析

出现这样的情况,主要原因是Windows的系统设置不同导致的问题。弹出框会根据系统的菜单位置设置的不同弹出的参考点会相应改变。
查看设置的方法:
使用组合键“Win+R”,调出“运行”对话框,在文本框中输入“shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}”
在这里插入图片描述
打开后选择其他。发现大部分系统默认为左手打开。但是当有些系统被人为的设置为右手打开时,Popup的弹出位置就异常了。
在这里插入图片描述

解决方法

方法一、 修改系统配置
(1)手动修改
参考上面
(2)通过Win32 Api修改

        [DllImport("user32.dll", EntryPoint = "SystemParametersInfo", SetLastError = true)]
        public static extern bool SystemParametersInfoSet(uint action, uint uiParam, uint vparam, uint init);
        void SetMenuAlign()
         {
         //uiParam为false时设置弹出菜单左对齐,true则右对齐
         SystemParametersInfoSet(0x001C /*SPI_SETMENUDROPALIGNMENT*/, 0, 0, 0);
         }

方法二、调整代码
采用 Placement="Absolute"的方式弹出Popup即可:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="360" Width="640">
    <Grid>
        <ToggleButton x:Name="Btn_Popup"  Width="70" Height="35"  Content="点击弹出"  />
        <Popup  x:Name="Popup1" 
                            Placement="Absolute"
                            AllowsTransparency="True"
                            PlacementTarget="{Binding ElementName=Btn_Popup}" 
                            IsOpen="{Binding IsChecked, ElementName=Btn_Popup}"
                            StaysOpen="False"
                            Width="120" 
                            Height="120"
                            HorizontalOffset="80" 
                            VerticalOffset="-100"    
                            Opened="Popup1_Opened"      
                            >
            <Grid>
                <Border   Width="120" Height="60" BorderBrush="#333333" BorderThickness="1" CornerRadius="10" Background="White" >
                    <TextBlock Text="弹出框"  FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center" />
                </Border>
            </Grid>
        </Popup>
    </Grid>
</Window>

在cs中计算相对位置:

 private void Popup1_Opened(object sender, EventArgs e)
        {
            Popup pop = sender as Popup;
            if (pop == null)
                return;
            if (pop.PlacementTarget == null)
                return;
            if (pop.Placement == PlacementMode.Absolute)
            {
                var relative = pop.PlacementTarget.PointToScreen(new Point(0, 0));
                pop.HorizontalOffset = relative.X + 80;
                pop.VerticalOffset = relative.Y + -100;
            }
        }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeOfCC

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值