2012-05-23 105 views
6

我希望在XAML中使用ImageBrush將背景應用於Grid使用ImageBrush的網格背景圖像

我給了筆刷x:Key,並希望在我的網格中引用它。

不幸的是,它沒有把圖像作爲背景。

<Window.Resources> 
    <ImageBrush ImageSource="/MAQButtonTest;component/images/bird_text_bg.jpg" x:Key="BackgroundSponge" /> 
    <Style TargetType="TextBlock"> 
     <Setter Property="OverridesDefaultStyle" Value="True"/> 
    </Style> 
    <ControlTemplate TargetType="Button" x:Key="ButtonTemplate"> 
     <Grid Width="444" ShowGridLines="False" SnapsToDevicePixels="True" Background="{DynamicResource BackgroundSponge}"> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="51" /> 
       <RowDefinition Height="36" /> 
      </Grid.RowDefinitions> 
      <Grid Grid.Row="0" Background="#286c97"> 

      </Grid> 
      <Grid Grid.Row="1" Background="#5898c0"> 
       <ContentPresenter Grid.Row="0" /> 
      </Grid> 
     </Grid> 
    </ControlTemplate> 
</Window.Resources> 

我想我大概指的是它在錯誤的道路,我已經試過DynamicResourceStaticResource

+0

你怎麼包括項目中的背景圖片?如果它被包含爲'Content',那麼我會期望你的ImageSource看起來更像這樣:''ImageSource =「pack:// application:,,,/component/images/bird_text_bg.jpg」' – paul

+1

如果你直接指定它(而不是使用資源)是否顯示? – Tim

+0

這是奇怪的事情。我已將它設置爲內容並在Visual Studio中具有圖像。我只是在Visual Studio中使用屬性窗格中的「ImageSource」的橢圓按鈕,然後它爲我自動生成該路徑。 – Luke

回答

2

在你的主網架你有涵蓋所有外網的可用空間,這就是爲什麼你將無法看到背景內孩子的。

<Grid Width="444" 
      Height="500" 
      Background="{DynamicResource BackgroundSponge}" 
      ShowGridLines="False" 
      SnapsToDevicePixels="True"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="51" /> 
      <RowDefinition Height="36" /> 
     </Grid.RowDefinitions> 
     <Grid Grid.Row="0" Background="#286c97" Opacity="0.2" Margin="5"/> 
     <Grid Grid.Row="1" Background="#5898c0" Opacity="0.2" Margin="5"> 
      <ContentPresenter Grid.Row="0" /> 
     </Grid> 
    </Grid> 

只有寬度沒問題,但高度怎麼樣?如果你只是讓高度大於你的孩子的物品,它會顯示出來。

或更好在裏面孩子有餘量。

保證金= 「5」

或使內在的小孩透明狀

透明度= 「0.2」

+0

謝謝!我瘋了:) – Luke

+0

當我使用絕對路徑在我的硬盤上映像時,它們是否會自動導出? – BlueWizard

+0

任何外部資源都不會被導出,因爲它們不存在於內部對象樹中。 – JSJ

3

我總是這樣做;

<Grid> 
    <Grid.Background> 
     <ImageBrush ImageSource="/Resources/Images/BG_BlankOptimized.png"/> 
    </Grid.Background> 
</Grid> 

或者,如果使用更像是保羅的StaticResource使用調用一個風格建議的圖像路徑的圖像刷資源調用它。

9

我常用這個。如果圖像作爲資源添加到項目中,請相對於此參照它們。

<ImageBrush x:Key="play" ImageSource="../Images/Buttons/Play.png" /> 

然後引用圖像畫筆:

<Border Background="{StaticResource play}"/> 
+0

我會補充一點,對我來說,最重要的部分是「如果圖像作爲資源添加到項目中」的部分,因爲在我的情況下,它們是內容,這就是爲什麼我有問題。謝謝! – Nikola