2015-09-05 82 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,或者將其作爲網格的第一個子元素)