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

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

posted @ 2015-04-14 23:16  玖彩技术团队  阅读(791)  评论(0编辑  收藏  举报
以上只是个人想法和实践经验,如果有文字错误和语法错误,请加以指点! QQ:247039968 emil:wujc@younger.com 无论是美女的歌声,还是鬣狗的狂吠,无论是鳄鱼的眼泪,还是恶狼的嚎叫,都不会使我动摇