2012-12-13 34 views
1

這裏內邊框背景是在我的應用程序資源的XAML,全球改變了所有的Button控件的應用程序的外觀和行爲像我想:更改支持算法FMP的ControlTemplate

<Style TargetType="{x:Type Button}" x:Key="MyButtonStyle"> 
    <Setter Property="SnapsToDevicePixels" Value="true"/> 
    <Setter Property="OverridesDefaultStyle" Value="true"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Border x:Name="Border" CornerRadius="0" BorderThickness="0" 
          Background="CornflowerBlue" BorderBrush="CornflowerBlue"> 
        <ContentPresenter Margin="2" HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" /> 
       </Border> 
       <ControlTemplate.Triggers> 
        <!-- a bunch o' triggers here --> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

在我的應用程序的一個用戶控件,我想改變這個按鈕的一些屬性。下面是一些XAML,我使用的UserControl.Resources部分現在要做到這一點:

<Style x:Key="SpecialButton" TargetType="Button" BasedOn="{StaticResource MyButtonStyle}"> 
    <Setter Property="Width" Value="20" /> 
    <Setter Property="Visibility" Value="Collapsed" /> 
    <Setter Property="Content" Value=">" /> 
    <Setter Property="Border" Value="#eeeeee" /> 
    <Setter Property="Border.Background" Value="#eeeeee" /> 
</Style> 

,我的風格分配給SpecialButton有正確的寬度,可見性和內容上我的用戶控制按鈕,但最後兩次嘗試不起作用。我將如何去改變這個SpecialButton風格中來自應用程序資源名稱爲「Border」的Border的背景顏色?

回答

2

你可以做的是使用TemplateBinding在基礎樣式中設置控件的背景屬性。同樣在基礎樣式中,將背景顏色設置爲默認的「CornflowerBlue」。

<Setter Property="Background" Value="CornflowerBlue" /> 
<Setter Property="Template"> 
    <Setter.Value>  
     <ControlTemplate TargetType="{x:Type Button}"> 
      <Border x:Name="Border" Background="{TemplateBinding Background}" 

現在,您可以覆蓋在派生的風格背景:

<Style x:Key="SpecialButton" TargetType="Button" BasedOn="{StaticResource MyButtonStyle}"> 
    <Setter Property="Background" Value="#eeeeee" /> 

(需要注意的是,如果你想使用不上的按鈕控件定義其他屬性 - 或者說,如果你想使用多種背景顏色 - 那麼你必須創建自己的控件,繼承Button,並將新屬性公開爲依賴屬性。)

+1

感謝您的回答,它不完全正確,但它導致我走向正確的道路。我原來的問題在第一個XAML代碼片段中的Style元素上有一個'x:Key',這不在我的應用程序的XAML中。因此,我不得不在我的第二個XAML代碼片段中使用'BasedOn =「{StaticResource {x:Type Button}}」'來讓它尊重對原始樣式的更改。 –

相關問題