2012-12-29 162 views
9

是否有可能在xaml中爲整個網格提供背景圖像和顏色?我沒有縮放圖像,因此有些區域沒有顏色。是否有可能爲網格的其餘部分着色?帶背景圖像和顏色的網格

這是我當前的代碼:

<Grid> 
      <Grid.Background> 
       <ImageBrush Stretch="None" ImageSource="Images/background_top.png" AlignmentY="Top" AlignmentX="Center"/> 
      </Grid.Background> 

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

回答

0

你的意思是這樣的:

<Grid Background="Red"> 
     <Grid.Background> 
      <ImageBrush Stretch="None" ImageSource="Images/background_top.png" AlignmentY="Top" AlignmentX="Center"/> 
     </Grid.Background> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="50*" /> 
     <RowDefinition Height="50*" /> 
    </Grid.RowDefinitions> 
</Grid> 
+11

這是行不通的,你設置兩次Background屬性。 –

8

,我能想到的唯一的辦法是使用背景屬性來設置顏色,然後將圖像添加到網格,確保跨越所有行和列。只要圖像是網格中的第一個項目,其他項目就會分層放置。我相信它會給你你正在尋找的效果。

<Grid Background="Red"> 
    <Image Grid.RowSpan="2" Stretch="None" Source="Images/background_top.png" VerticalAlignment="Top" HorizontalAlignment="Center"/> 
    <Label Content="Label" Grid.Row="0" Height="28" HorizontalAlignment="Center" Margin="10,10,0,0" Name="label1" VerticalAlignment="Top" /> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="50*" /> 
     <RowDefinition Height="50*" /> 
    </Grid.RowDefinitions> 
</Grid> 
4

您可以嘗試在網格周圍使用邊框和所需顏色作爲背景顏色。

<Border Background="Red"> 
    <Grid> 
     <Grid.Background> 
      <ImageBrush Stretch="None" ImageSource="Images/background_top.png" AlignmentY="Top" AlignmentX="Center"/> 
     </Grid.Background> 

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

在WPF中,如果你想定義既具有PNG和顏色與VisualBrush(這個刷一刷,你甚至可以做到這一點 - 和不少其他刷無法在Windows應用商店的應用程序,由於性能損失當渲染刷) 這是一個基本的例子,刷有相當多的屬性,你可以玩的:

<Window.Resources> 
    <VisualBrush x:Key="myBrush"> 
     <VisualBrush.Visual> 
      <Grid> 
       <Rectangle Fill="Red"/> 
       <Image Source="troll.png"/> 
      </Grid> 
     </VisualBrush.Visual> 
    </VisualBrush> 
</Window.Resources> 
<Grid Background="{StaticResource myBrush}"/> 
0

不完全回答你的問題,但要獲得類似的視覺效果,你可以將網格的背景設置爲圖像;和你的頁面/窗口的背景顏色。

0

將網格放入另一個網格中。外部網格有一個SolidColorBrush,內部網格有一個部分透明的ImageBrush。