2011-03-11 88 views
3

嘿我試圖在WPF中設置樣式按鈕。我有一個基本樣式:WPF BasedOn造型

<Style x:Key="buttonStyle" TargetType="{x:Type ButtonBase}"> 
    <Setter Property="FontWeight" Value="Bold"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate x:Name="ButtonBaseControlTemplate" TargetType="{x:Type ButtonBase}"> 
       <Grid SnapsToDevicePixels="True"> 
        <Border x:Name="Bd" Background="#9BC4E2" BorderBrush="Black" 
          BorderThickness="3,3,3,3" CornerRadius="7,7,7,7" Padding="5,5,5,5"> 
         <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="Content" 
             ContentSource="Content" 
             RecognizesAccessKey="True" HorizontalAlignment="Center" VerticalAlignment="Center"/> 
        </Border> 
       </Grid> 
      <-- Removed triggers here --> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

但我需要用這個切換按鈕也僅僅是用來改變背景器isChecked顏色。但我不能訪問邊界設置背景,當我嘗試這樣的:

<Style TargetType="{x:Type Button}" BasedOn="{StaticResource buttonStyle}"> 
</Style> 

<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource buttonStyle}"> 
    <Style.Triggers> 
     <Trigger Property="IsChecked" Value="True"> 
      <Setter Property="Background" Value="White"/> <-- Need to specify target name of the border here somehow --> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

有什麼建議嗎? TA

回答

9

這差不多就是TemplateBinding是專爲

http://devlicio.us/blogs/christopher_bennage/archive/2008/07/04/templatebinding-a-bridge-between-styles-and-templates.aspx

更改Border.Background屬性爲

Background="{TemplateBinding Background}" 

這二傳手添加到您的按鈕樣式風格

<Setter Property="Background" Value="#9BC4E2" /> 

然後你可以宣佈你的ToggleButton這樣的

<ToggleButton Content="ToggleButton" Background="Green" /> 
+0

這很好,謝謝,我們有一個工作的解決方案! – 2011-03-11 16:24:51