2010-05-13 12 views
6

Tôi có một bộ nút bên trong ngăn xếp ngăn xếp. Tôi muốn tất cả họ đều có hình nền. Làm thế nào tôi có thể làm điều đó bằng cách sử dụng phong cách? vì tôi không muốn đặt thủ công hình nền cho mỗi nút.Đặt hình nền điều khiển WPF bằng cách sử dụng kiểu?

Đây là một đoạn mã:

<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> 

Cảm ơn.

Trả lời

14

Vâng, bạn chỉ định trình thiết lập cho thuộc tính Background trong kiểu và bạn đặt giá trị của nó thành 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

Dưới đây là kiểu cho nút và hình ảnh backgroung được đặt thành nó.Bạn có thể thay đổi nguồn của ImageBrush thành hình bạn muốn.

<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> 

Và sau đó chỉ cần sử dụng nó:

<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

Infact chiều cao, chiều rộng hoặc bất kỳ tài sản mà bạn có thể thiết lập nó theo phong cách của một nút và sau đó chỉ cần áp dụng các phong cách để nút của bạn – Malcolm