UserControl子填充窗口

时间:2016-03-15 14:52:42

标签: wpf user-controls window wpf-controls

我有一个UserControl,它是wpf窗口的一部分。

<Window>
    <Grid>
        <!--some other display elements would be here-->
        <local:MyUserControl x:Name="Foo" Padding="0,42,0,50"/>
    </Grid>
</Window>

在MyUserControl中我有一个通常隐藏的图库元素,但是当它可见时,它应该填满整个屏幕。

<UserControl>
    <Grid>
        <!--main display elements would be here-->
        <Grid Name="Gallery" Visibility="Hidden">
            <Rectangle Fill="Black" Opacity="0.75"/>
            <TextBlock Name="GalleryLabel" Foreground="White" TextAlignment="Center">Current Image Title</TextBlock>
            <Button Name="CloseGallery" Style="{DynamicResource WhiteTextButton}" Margin="0,0,0,0" Height="25" VerticalAlignment="Top" HorizontalAlignment="Right" Width="25" Click="GalleryClose_OnClick">X</Button>
            <Image Name="GalleryImage" Margin="25"/>
        </Grid>
    </Grid>
</UserControl>

如何设置Gallery以填充整个Window而不仅仅是UserControl?

我能够通过将Margin="0,-42,0,-50"添加到图库来让它工作,但我不喜欢这个解决方案。我宁愿做一些不会在UserControl中涉及硬编码值的事情,这样我就可以更灵活地使用它。

通常看起来像: Normal Window 其中绿色Foo区域是MyUserControl,窗口中的其他内容是其他元素。

在某些时候,我有一个图库显示一个图像,它应该填满整个屏幕,如: enter image description here 它应该填满整个屏幕,并有一个黑色的不透明覆盖图,以及显示在覆盖图顶部的图像。

2 个答案:

答案 0 :(得分:0)

删除填充...

您可以使用:

 <local:MyUserControl x:Name="Foo" VerticalAlignment="Stretch" HorizontalAligment="Stretch"/>

修改

我可能承认我仍然不确定你想要什么。但是我做了一些与你在图片中展示的相同的东西。但请注意,这是一个基于意见的问题,答案是我的意见,有很多方法可以实现这一点。而这只是其中之一:

<强>主窗口

<Window x:Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:myProject="clr-namespace:MyProject"
    Title="MainWindow" >

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Border Background="Gainsboro" Grid.Row="0">
        <StackPanel Orientation="Horizontal">
            <Button Content="Show gallery" Width="100" Margin="10"/>
            <Button Content="Do something else" Width="150" Margin="10"/>
        </StackPanel>
    </Border>

    <myProject:SomeOtherStuff Grid.Row="1" />

    <Border Grid.Row="2" Background="Gainsboro">
        <StackPanel Orientation="Horizontal">
            <Label Content="Buttom area, can be used for something else"/>
        </StackPanel>
    </Border>

    <myProject:GalleryUserControl x:Name="GalleryUserControl" Grid.Row="0" Grid.RowSpan="3" Visibility="Hidden"/>
</Grid>

SomeOtherStuff UserControl

<UserControl x:Class="MyProject.SomeOtherStuff"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Background="Green">
    <Label VerticalAlignment="Center" HorizontalAlignment="Center" Content="Foo" FontSize="30" FontFamily="Verdana"/>
</Grid>

图库用户控件

<UserControl x:Class="XamDataGrid.GalleryUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="500" Width="800">
<Grid Background="#99000000">
    <TextBlock Text="Currect image title" Foreground="White" TextAlignment="Center" VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
    <Button Name="CloseGallery"  Margin="0,0,0,0" Content="X" Height="25" VerticalAlignment="Top" HorizontalAlignment="Right" Width="25" Click="GalleryClose_OnClick"/>
    <Image Margin="30"/>
</Grid>

答案 1 :(得分:0)

Adorner最适合您。因为它漂浮在其他一切之上并且仍然在VisualTree之外。

Overlay Gallery Control Output

UserControl2

  <UserControl2 ...>
   <Grid>
        <Grid Background="#FF709FA6" Opacity="0.3" Width="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}"
             Height="{Binding ActualHeight, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}">
        </Grid>
        <Grid Width="600" Height="700">
            <Grid.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF37DAEA" Offset="0"/>
                    <GradientStop Color="#FFE84242" Offset="1"/>
                </LinearGradientBrush>
            </Grid.Background>
            <!-- Gallery controls -->
            <Button Content="Welcome to Gallery" HorizontalAlignment="Left" IsHitTestVisible="True" Margin="112,126,0,0" VerticalAlignment="Top" Height="68" FontSize="48" Click="Button_Click_1"/>
            <TextBlock HorizontalAlignment="Left" Margin="83,42,0,0" TextWrapping="Wrap" Text="UserControl2" VerticalAlignment="Top" Foreground="#FF1B0D0D"/>
        </Grid>
    </Grid>
  </UserControl2>

的UserControl1

代码:

public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // get root Window
            DependencyObject current = LogicalTreeHelper.GetParent(this);
            while (!(current is Window))
                current = LogicalTreeHelper.GetParent(current);

            Window root = current as Window;

            IEnumerable children = LogicalTreeHelper.GetChildren(root);
            Panel p = null;
            foreach (var child in children)
            {
                p = child as Panel; // get first container panel in Window
                break;
            }

            // Create UserControl2 to add to the adorner
            UserControl2 ctrlGallery = new UserControl2();

            AdornerLayer layer = AdornerLayer.GetAdornerLayer(p);
            GalleryAdorner adorner = new GalleryAdorner(p, ctrlGallery);
            layer.Add(adorner);
        }
    }

public class GalleryAdorner : Adorner
    {
        private Control _child;
        VisualCollection collection;

        public GalleryAdorner(UIElement elem, Control child)
            : base(elem)
        {
            collection = new VisualCollection(this);
            _child = child;
            collection.Add(_child);
        }

        protected override int VisualChildrenCount
        {
            get
            {
                return 1;
            }
        }

        protected override Visual GetVisualChild(int index)
        {
            if (index != 0) throw new ArgumentOutOfRangeException();
            return collection[0];
        }

        protected override Size MeasureOverride(Size constraint)
        {
            _child.Measure(constraint);
            return _child.DesiredSize;
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            _child.Arrange(new Rect(new Point(0, 0), finalSize));
            return new Size(_child.ActualWidth, _child.ActualHeight);
        }
    }