欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C# wpf解決Popup彈出位置異常問題解決

 更新時(shí)間:2021年10月18日 15:47:23   作者:Alfred-N  
本文主要介紹了C# wpf解決Popup彈出位置異常問題解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

問題描述

使用Popup控件作為彈出框,使用相對(duì)位置彈出即Placement=“Relative”,在不同的設(shè)備中彈出的位置不一致。比如下面的例子。

使用如下代碼:

<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>

顯示效果:

在這里插入圖片描述

在這里插入圖片描述

原因分析

出現(xiàn)這樣的情況,主要原因是Windows的系統(tǒng)設(shè)置不同導(dǎo)致的問題。彈出框會(huì)根據(jù)系統(tǒng)的菜單位置設(shè)置的不同彈出的參考點(diǎn)會(huì)相應(yīng)改變。

查看設(shè)置的方法:

使用組合鍵“Win+R”,調(diào)出“運(yùn)行”對(duì)話框,在文本框中輸入“shell:::{80F3F1D5-FECA-45F3-BC32-752C152E456E}”

在這里插入圖片描述

打開后選擇其他。發(fā)現(xiàn)大部分系統(tǒng)默認(rèn)為左手打開。但是當(dāng)有些系統(tǒng)被人為的設(shè)置為右手打開時(shí),Popup的彈出位置就異常了。

在這里插入圖片描述

解決方法

方法一、 修改系統(tǒng)配置
(1)手動(dòng)修改
參考上面
(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時(shí)設(shè)置彈出菜單左對(duì)齊,true則右對(duì)齊
         SystemParametersInfoSet(0x001C /*SPI_SETMENUDROPALIGNMENT*/, 0, 0, 0);
         }

方法二、調(diào)整代碼
采用 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="點(diǎn)擊彈出"  />
        <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中計(jì)算相對(duì)位置:

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

到此這篇關(guān)于C# wpf解決Popup彈出位置異常問題解決的文章就介紹到這了,更多相關(guān)C# Popup彈出位置異常內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論