2010-05-13 26 views
6

我有一組堆棧面板內的按鈕。我希望他們都有一個背景圖像。我怎樣才能使用樣式?因爲我不想手動設置每個按鈕的背景圖像。使用樣式設置WPF控件背景圖像?

下面的代碼片段:

<StackPanel Orientation="Horizontal" Height="100px" VerticalAlignment="Top"> 
     <StackPanel.Resources> 
      <Style TargetType="Button"> 
       <Setter Property="Margin" Value="2,4" /> 
      </Style> 
     </StackPanel.Resources> 
     <Button Width="127px" Height="79px" VerticalAlignment="Bottom"> 
      <Button.Background> 
       <ImageBrush ImageSource="images/Tab.png" /> 
      </Button.Background> 
     </Button> 
     <Button>A</Button> 
     <Button>R</Button> 
     <Button>S</Button> 
    </StackPanel> 

感謝。

回答

14

那麼,你需要爲樣式中的Background屬性指定一個setter,並將其值設置爲ImageBrush

<StackPanel Orientation="Horizontal" Height="100px" VerticalAlignment="Top"> 
     <StackPanel.Resources> 
      <Style TargetType="Button"> 
       <Setter Property="Margin" Value="2,4"/> 
       <Setter Property="Background"> 
        <Setter.Value> 
        <ImageBrush ImageSource="images/Tab.png"/> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </StackPanel.Resources> 

     <Button Width="127px" Height="79px" VerticalAlignment="Bottom"/> 
     <Button>A</Button> 
     <Button>R</Button> 
     <Button>S</Button> 
    </StackPanel> 
1

下面是樣式的按鈕,backgroung圖像設置it.You可以在圖像刷的源改變到你想要的人。

<Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> 
      <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/> 
      <Setter Property="Background" > 
       <Setter.Value> 
        <ImageBrush ImageSource="pic.png"></ImageBrush> 
       </Setter.Value> 
      </Setter> 

      <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/> 
      <Setter Property="BorderThickness" Value="1"/> 
      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
      <Setter Property="HorizontalContentAlignment" Value="Center"/> 
      <Setter Property="VerticalContentAlignment" Value="Center"/> 
      <Setter Property="Padding" Value="1"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true"> 
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Microsoft_Windows_Themes:ButtonChrome> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsKeyboardFocused" Value="true"> 
           <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/> 
          </Trigger> 
          <Trigger Property="ToggleButton.IsChecked" Value="true"> 
           <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/> 
          </Trigger> 
          <Trigger Property="IsEnabled" Value="false"> 
           <Setter Property="Foreground" Value="#ADADAD"/> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

,然後只用它:

<StackPanel Orientation="Horizontal" Height="100px" VerticalAlignment="Top"> 
     <StackPanel.Resources> 
      <Style TargetType="Button"> 
       <Setter Property="Margin" Value="2,4" /> 
      </Style> 
     </StackPanel.Resources> 
     <Button Width="127px" Height="79px" VerticalAlignment="Bottom" Style="{StaticResource ButtonStyle1}"> 
     </Button> 
     <Button Style="{StaticResource ButtonStyle1}" >A</Button> 
     <Button Style="{StaticResource ButtonStyle1}">R</Button> 
     <Button Style="{StaticResource ButtonStyle1}">S</Button> 
    </StackPanel> 
+0

逸岸的高度,寬度或任何屬性,你可以將其設置在一個按鈕的樣式,然後只是應用樣式到您的按鈕 – Malcolm 2010-05-13 14:32:41