C# wpf解決Popup彈出位置異常問題解決
問題描述
使用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)文章
C#采用Winform實(shí)現(xiàn)類似Android的Listener
這篇文章主要介紹了C#采用Winform實(shí)現(xiàn)類似Android的Listener,很實(shí)用的技巧,需要的朋友可以參考下2014-08-08C#中IEnumerable、ICollection、IList、List之間的區(qū)別
IEnumerable、ICollection、IList、List之間的區(qū)別,本文分別分析了它的實(shí)現(xiàn)源碼,從而總結(jié)出了它們之間的關(guān)系和不同之處。對(duì)C# IEnumerable、ICollection、IList、List相關(guān)知識(shí),感興趣的朋友一起看看吧2021-07-07C#最簡(jiǎn)單的關(guān)閉子窗體更新父窗體的實(shí)現(xiàn)方法
原理就是將子窗體最為對(duì)話框模式彈出,當(dāng)窗體關(guān)閉或取消時(shí)更新主窗體2012-11-11C#統(tǒng)計(jì)字符串里中文漢字個(gè)數(shù)的方法
這篇文章主要介紹了C#統(tǒng)計(jì)字符串里中文漢字個(gè)數(shù)的方法,本文通過正則實(shí)現(xiàn)統(tǒng)計(jì)出一段字符串里中文字?jǐn)?shù),需要的朋友可以參考下2014-08-08selenium.chrome寫擴(kuò)展攔截或轉(zhuǎn)發(fā)請(qǐng)求功能
Selenium?WebDriver?是一組開源?API,用于自動(dòng)測(cè)試?Web?應(yīng)用程序,利用它可以通過代碼來控制chrome瀏覽器,今天通過本文給大家介紹selenium?chrome寫擴(kuò)展攔截或轉(zhuǎn)發(fā)請(qǐng)求功能,感興趣的朋友一起看看吧2022-07-07