2016-03-15 79 views
0

我有一个用户控件,它是一个wpf窗口的一部分。UserControl子窗口填充

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

Inside 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> 

如何设置图库来填充整个窗口,而不仅仅是UserControl?

我可以通过将Margin="0,-42,0,-50"添加到图库中,但我不喜欢该解决方案。我宁愿做一些不涉及在UserControl中对值进行硬编码的东西,这样我就可以在使用它的时候拥有更多的灵活性。

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

在某些点,我有一个廊显示图像,这应该填补像整个屏幕: enter image description here 应填满整个屏幕并有黑色不透明的覆盖,与图像一起在覆盖的顶部显示。

回答

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 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> 

+0

但问题是现在的MyUserControl覆盖了窗口 – DeadEli

+0

其他元素是不是有什么你要 ???我们无法看到您到达那里的其他元素。并且您要求填写整个窗口 –

+0

不。我有'<! - 其他一些显示元素会在这里 - >'在窗口中,因为我可能没有实际的元素,它们是主题改变。我特意要求画廊填满窗口,而不是其他任何东西。 – DeadEli

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); 
     } 
    }