2013-04-25 51 views
1

                                            MediaElementWPF:爲什麼PopUp中的MediaElement無法調整到屏幕高度?

我有一個PopUpMediaElement控制。當我將MediaElement的大小調整爲寬屏幕時,會在底部切割一個區域(請參閱圖像)。

我該怎麼做?如何解決它?非常感謝你!

XAML:

<Popup PlacementRectangle="-500,0,0,0" Placement="Relative" IsOpen="True" Name="popup"> 
     <MediaElement Name="me" Width="480" Height="360" Volume="1" 
      MouseLeftButtonUp="me_MouseLeftButtonUp"/> 
    </Popup> 

代碼:

bool fullscreen = false; 
    private void me_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
    { 
     fullscreen = !fullscreen; 
     if (fullscreen) 
     { 
      popup.PlacementRectangle = new Rect(0, 0, 0, 0); 
      popup.Placement = PlacementMode.Absolute; 
      me.Width = Screen.PrimaryScreen.Bounds.Width; 
      me.Height = Screen.PrimaryScreen.Bounds.Height; 
     } 
     else 
     { 
      popup.PlacementRectangle = new Rect(-500, 0, 0, 0); 
      popup.Placement = PlacementMode.Relative; 
      me.Width = 480; 
      me.Height = 360; 
     } 
    } 

回答

2

我發現這個問題的解決方法。 只需搜索可視化樹,從彈出窗口的子項開始,直到找到PopupRoot類型的父項。你可以將它的寬度和高度設置爲你喜歡的任何東西。這對我有效:

for (var parent = Child as FrameworkElement; parent != null; parent = VisualTreeHelper.GetParent(parent) as FrameworkElement) 
{ 
    if (parent.GetType().Name == "PopupRoot") 
    { 
     parent.Width = Target.ActualWidth; 
     parent.Height = Target.ActualHeight; 
     break; 
    } 
}