服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# wpf解决Popup弹出位置异常问题解决

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

2022-12-07 11:30Alfred-N C#

本文主要介绍了C# wpf解决Popup弹出位置异常问题解决,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

问题描述

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

使用如下代码:

?
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
<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# wpf解决Popup弹出位置异常问题解决

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

原因分析

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

查看设置的方法:

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

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

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

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

解决方法

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

?
1
2
3
4
5
6
7
[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即可:

?
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
<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中计算相对位置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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弹出位置异常内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/u013113678/article/details/120696007

延伸 · 阅读

精彩推荐
  • C#C#怎样实现文件下载断点续传

    C#怎样实现文件下载断点续传

    这篇文章主要介绍了C#怎样实现文件下载断点续传,对断点续传感兴趣的同学,可以参考下...

    农码一生7522022-11-15
  • C#webBrowser执行js的方法,并返回值,c#后台取值的实现

    webBrowser执行js的方法,并返回值,c#后台取值的实现

    下面小编就为大家带来一篇webBrowser执行js的方法,并返回值,c#后台取值的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过...

    C#教程网11192021-12-13
  • C#C#编程之事务用法

    C#编程之事务用法

    这篇文章主要介绍了C#编程之事务用法,结合实例形式对比分析了C#中事务提交与回滚的具体实现技巧与相关注意事项,具有一定参考借鉴价值,需要的朋友可以...

    mil4012021-11-03
  • C#unity shader实现玻璃折射效果

    unity shader实现玻璃折射效果

    这篇文章主要为大家详细介绍了unity shader实现玻璃折射效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    周者4912022-09-05
  • C#C#连接SQL Server的实现方法

    C#连接SQL Server的实现方法

    这篇文章主要给大家介绍了关于C#连接SQL Server的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    _Mrchen_11972022-03-06
  • C#C#创建Excel多级分组的方法

    C#创建Excel多级分组的方法

    这篇文章主要为大家详细介绍了C#创建Excel多级分组的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    E-iceblue7892022-02-17
  • C#C#泛型的使用及示例详解

    C#泛型的使用及示例详解

    这篇文章主要介绍了C#泛型的使用及示例,本文通过例子个大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...

    .NET开发菜鸟8892022-11-29
  • C#C# Mqtt 断线重连的实现代码

    C# Mqtt 断线重连的实现代码

    这篇文章主要介绍了C# Mqtt 断线重连,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来...

    时间维度3332022-08-04