iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C# wpf解决Popup弹出位置异常问题解决
  • 614
分享到

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

2024-04-02 19:04:59 614人浏览 泡泡鱼
摘要

目录问题描述原因分析解决方法问题描述 使用Popup控件作为弹出框,使用相对位置弹出即Placement=“Relative”,在不同的设备中弹出的位置不一致。比如下面的例子。 使

问题描述

使用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 , 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# wpf解决Popup弹出位置异常问题解决的文章就介绍到这了,更多相关C# Popup弹出位置异常内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C# wpf解决Popup弹出位置异常问题解决

本文链接: https://www.lsjlt.com/news/154749.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作