问题描述

使用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;
            }
        }

到此这篇关于C# wpf解决Popup弹出位置异常问题解决的文章就介绍到这了,更多相关C# Popup弹出位置异常内容请搜索悠悠之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠悠之家!

点赞(81)

评论列表共有 0 条评论

立即
投稿
返回
顶部