我可以从Visual Tree中删除子窗口并将其重新放入以实现FullScreen吗?

时间:2011-08-16 19:08:12

标签: silverlight childwindow visual-tree

我有一个ChildWindow的弹出窗口。在弹出窗口中我有一个UserControl(MediaPreviewView),它有一个MediaElement和一些按钮来控制视频。我的要求声明我需要一个全屏按钮,并在用户点击按钮时显示全屏视频。

我正在使用MVVM,所以我决定尝试使用MVVM Light中的Messaging。我正在向我的基本视图发送消息。在View的代码隐藏中,我正在显示一个Grid(隐藏在XAML的底部,具有高zindex)。我的消息包含MediaPreviewControl,我正在设置Grid.Children.Add(对于控件。我已经尝试了多个东西,可以让ChildWindow看不见,但按钮不起作用。似乎是ChildWindow即使宽度和高度为0,仍然在按钮的顶部。 是否有一个更好的是一个可行的方法来使我的MediaPreviewView全屏?

public class MediaPreviewFullScreenMessage
{
    public MediaPreviewView PreviewView { get; set; }
    public ChildWindow ContainerChildWindow { get; set; }
    public bool ChangeToFullScreen { get; set; }
}

// Register for FullScreen media preview
Messenger.Default.Register<MediaPreviewFullScreenMessage>(this,
(fullScreenMessage) =>
{
  this.fullScreenHolderGrid.Visibility = fullScreenMessage.ChangeToFullScreen ? Visibility.Visible : Visibility.Collapsed;
  this.fullScreenHolderGrid.Children.Clear();
  if (fullScreenMessage.ChangeToFullScreen)
  {
// I've tried, Visibility, width and height = 0 on the fullScreenMessage.ContainerChildWindow, even a TranslateTransform
....
}
});

1 个答案:

答案 0 :(得分:0)

如何简单地最大化ChildWindow?在你的全屏Button后面,

    private void FullScreenButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        // get the explorer window size
        double height = App.Current.Host.Content.ActualHeight;
        double width = App.Current.Host.Content.ActualWidth;

        // need to make it a little bit smaller or it won't resize
        this.Height = height - 1;
        this.Width = width - 1;

        // need to update the layout here
        this.UpdateLayout();

        // the following code repositions the child window
        var root = VisualTreeHelper.GetChild(this, 0) as FrameworkElement;
        if (root == null)
        {
            return;
        }

        var contentRoot = root.FindName("ContentRoot") as FrameworkElement;
        if (contentRoot == null)
        {
            return;
        }

        var group = contentRoot.RenderTransform as TransformGroup;
        if (group == null)
        {
            return;
        }

        TranslateTransform translateTransform = null;
        foreach (var transform in group.Children.OfType<TranslateTransform>())
        {
            translateTransform = transform;
        }

        if (translateTransform == null)
        {
            return;
        }

        // reset transform
        translateTransform.X = 0.0;
        translateTransform.Y = 0.0;
    }

<强>更新

ChildWindow的默认样式中,Grid名为ContentRoot,其ChildWindow的位置设置为RenderTransform。这就是为什么你需要重置TranslateXTranslateY以使其左上角。

                        <Grid x:Name="ContentRoot" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" Height="{TemplateBinding Height}" RenderTransformOrigin="0.5,0.5" VerticalAlignment="{TemplateBinding VerticalAlignment}" Width="{TemplateBinding Width}">
                            <Grid.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform/>
                                    <SkewTransform/>
                                    <RotateTransform/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </Grid.RenderTransform>