2015-09-05 87 views
1

目前,我可以创建一个包含背景图片和kinect照片(都是图片标签)的网格标签。WPF使用网格作为背景

<Grid Name="CompositeImage"> 
    <Image Source="bg.png" Strech="Fill" HorizontalAlignment="Strech" VerticalAlignment="Strech"/> 
    <Image Source="{Binding kinectPhoto}" Strech="UniformToFill" HorizontalAlignment="Strech" VerticalAlignment="Strech"/> 
</Grid> 

但现在,我想使用这个网格作为背景,但我不能在背景标记下使用它。像这样

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="3*"/> 
     <ColumnDefinition Width="15*"/> 
     <ColumnDefinition Width="3*"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Width="3*"/> 
     <RowDefinition Width="8*"/> 
     <RowDefinition Width="3*"/> 
    </Grid.RowDefinitions> 

    <Grid.Background> 
     <Grid Name="CompositeImage"> ... same as above ... </Grid> 
    </Grid.Background> 

    <!-- other components --> 
</Grid> 

是否有任何解决方案使网格CompositeImage作为背景或使其适合外部网格?

回答

1

你可以只让“背景”网格跨度通过设置Grid.RowSpanGrid.ColumnSpan属性的所有行和列:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="3*"/> 
     <ColumnDefinition Width="15*"/> 
     <ColumnDefinition Width="3*"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Width="3*"/> 
     <RowDefinition Width="8*"/> 
     <RowDefinition Width="3*"/> 
    </Grid.RowDefinitions> 

    <Grid x:Name="CompositeImage" Grid.ColumnSpan="3" Grid.RowSpan="3"> 
     ... 
    </Grid> 

    <!-- other components --> 
</Grid> 
0

实际上你做了两个同名的网格CompositeImage。你想要做的是这样的:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="3*"/> 
     <ColumnDefinition Width="15*"/> 
     <ColumnDefinition Width="3*"/> 
    </Grid.ColumnDefinitions> 
    <Grid.RowDefinitions> 
     <RowDefinition Width="3*"/> 
     <RowDefinition Width="8*"/> 
     <RowDefinition Width="3*"/> 
    </Grid.RowDefinitions> 

    <Grid.Background> 
     <VisualBrush> 
      <VisualBrush.Visual> 
       <Grid> 
        <Image Source="bg.png" Strech="Fill" HorizontalAlignment="Strech" VerticalAlignment="Strech"/> 
        <Image Source="{Binding kinectPhoto}" Strech="UniformToFill" HorizontalAlignment="Strech" VerticalAlignment="Strech"/> 
       </Grid> 
      </VisualBrush.Visual> 
     </VisualBrush> 
    </Grid.Background> 

    <!-- other components --> 
</Grid> 
+0

您不能将网格(或任何其他UIElement)用作背景的值属性(它是Brush类型的)。您可能会使用VisualBrush。 – Clemens

0

如果你真的想用它作为Background,然后请注意,此属性需要刷机。

您可以构建一个VisualBrush,它将呈现您的网格与图像,然后使用这种画笔作为背景。但是,我认为你所希望的只是将它用作“背景”(而不是背景),在这种情况下,只需将它放入目标网格,然后“最大化”(colspan = xxxx rowspan = xxx水平=拉伸垂直=拉伸)并将其向后移动(zindex = somelowvalue,或者将其作为网格的第一个子元素)