WPF Popup全屏 弹出方法。解决只显示75%的问题。

2023-02-15,,,,

WPF Popup全屏 弹出方法解决只显示75%的问题。

 

WPF 中 Popup 有一个特点。当Popup的高度超过屏幕的75%的时候,只显示75%的高度。

如下代码:

<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
WindowState="Maximized" WindowStyle="None" >
<Grid>
<Button Content="Open" Click="OnClick" Width="100" Height="25" VerticalAlignment="Bottom" />
<Popup x:Name="_popup" Placement="Absolute" Width="1920" Height="1080" >
<Grid>
<Rectangle></Rectangle>
<TextBlock x:Name="_txt" Text="TestFullPopup" />
</Grid>
</Popup>
</Grid>
</Window>
    /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void OnClick(object sender, RoutedEventArgs e)
{
_popup.IsOpen = !_popup.IsOpen;
}
}

显示效果:

仔细看,下边是有个控钮的啊,大概只有75%的样式(我事先就知道了!嘿嘿!)。我们再看一下Snoop上的值。

真的只有75%, 1080 * 0.75 = 810。

大家可以试各种情况。高度超过75%,看看高度是不是只有75%。

我还试过,不从0,0 位置弹出 Popup,往下偏移一段距离。依然不可以。(试出来可以的,告诉我一下)

而且在CS中设置 Popup的大小也是无效的,我还试过开子线程,延迟一段时间再设置 Popup的高度也是无效的。

再看一下Snoop,我们既然能看到 PopupRoot的可视高度,那是不是可以修改,PopupRoot 的高度来显示。修改 Height为1080,成功可以全屏。

于是只要设置 PopupRoot.Height 属性就可以了!

首先想到就是用 VisualTreeHelper.GetChild(); 获取Popup的中的PopupRoot.

出现两个问题:1.使用VisualTreeHelper.GetChild(Popup),跟本拿不到 Popup的子控件。

       2.PopupRoot 可能是一个内部的类型。反正我是没找到!找到的同学请告诉我一下。

只好使用  VisualTreeHelper.GetParent(),由于没找到 PopupRoot 类,只好先使用字串的判断方法。"System.Windows.Controls.Primitives.PopupRoot",直接上代码:

   /// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void OnClick(object sender, RoutedEventArgs e)
{
_popup.IsOpen = !_popup.IsOpen; DependencyObject parent = _popup.Child; do
{
parent = VisualTreeHelper.GetParent(parent); if (parent != null && parent.ToString() == "System.Windows.Controls.Primitives.PopupRoot")
{
var element = parent as FrameworkElement; var mainWin = Application.Current.MainWindow; element.Height = mainWin.Height;
element.Width = mainWin.Width; break;
}
}
while (parent != null);
}
}

最终效果:

收工,睡觉。

可能没人会转,但转的一定要注明出处:http://www.cnblogs.com/gaoshang212/p/3157769.html

WPF Popup全屏 弹出方法。解决只显示75%的问题。的相关教程结束。

《WPF Popup全屏 弹出方法。解决只显示75%的问题。.doc》

下载本文的Word格式文档,以方便收藏与打印。