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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM