温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2021-10-19 09:11:52 来源:亿速云 阅读:151 作者:柒染 栏目:开发技术

C#如何解决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>

显示效果:

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

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

原因分析

出现这样的情况,主要原因是Windows的系统设置不同导致的问题。弹出框会根据系统的菜单位置设置的不同弹出的参考点会相应改变。

查看设置的方法:

使用组合键“Win+R”,调出“运行”对话框,在文本框中输入“shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}”

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

打开后选择其他。发现大部分系统默认为左手打开。但是当有些系统被人为的设置为右手打开时,Popup的弹出位置就异常了。

C#如何解决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;
            }
        }

关于C#如何解决Popup弹出位置异常问题问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI