2012-11-30 58 views
0

我一直在試圖創建一個用戶控件庫,它只是具有默認WPF控件的樣式版本。基本上,我在每個項目中都使用相同的控件風格。用戶控件按鈕 - 樣式 - WPF

所以我認爲,而不是restyling在每個項目中的一切,我可以創建一個用戶控制庫,將所有的控件預先設計。不過,我似乎無法將樣式應用於控件,因爲它會留下一個空白框。

這是按鈕控件的XAML。我知道我一定在做一些完全錯誤的事情,但我似乎無法在網絡上找到關於如何做到這一點的任何信息。

基本上,我只希望這是一個標準的WPF按鈕,但與「世紀的哥特式」的字體,點擊後綠色背景等

任何人都可以點我在正確的方向?

<Button x:Class="IM.WPF.Controls.imButton" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     mc:Ignorable="d" 
     d:DesignHeight="23" 
     d:DesignWidth="75"> 

    <Button.Style> 
    <Style TargetType="Button" 
      x:Name="imButtonStyle"> 
     <Setter Property="SnapsToDevicePixels" 
       Value="True" /> 
     <Setter Property="OverridesDefaultStyle" 
       Value="True" /> 
     <Setter Property="Content" 
       Value="imButton" /> 
     <Setter Property="BorderBrush" 
       Value="#006150" /> 
     <Setter Property="FontFamily" 
       Value="Century Gothic" /> 
     <Setter Property="FontSize" 
       Value="12" /> 
     <Setter Property="Foreground" 
       Value="#006150" /> 
     <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsPressed" 
         Value="True"> 
       <Setter Property="Background"> 
        <Setter.Value> 
        <LinearGradientBrush> 
         <GradientStop Color="#006150" 
            Offset="0" /> 
         <GradientStop Color="#006150" 
            Offset="0.5" /> 
         <GradientStop Color="#006150" 
            Offset="0.5" /> 
         <GradientStop Color="#006150" 
            Offset="0.9" /> 
        </LinearGradientBrush> 
        </Setter.Value> 
       </Setter> 
       </Trigger> 
      </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
     </Setter> 
    </Style> 
    </Button.Style> 

    <Button.Background> 
    <LinearGradientBrush StartPoint="0,0" 
         EndPoint="0,1"> 
     <GradientStop Color="White" 
        Offset="0" /> 
     <GradientStop Color="#E0E0E0" 
        Offset="0.5" /> 
     <GradientStop Color="LightGray" 
        Offset="0.5" /> 
     <GradientStop Color="DarkGray" 
        Offset="0.9" /> 
    </LinearGradientBrush> 
    </Button.Background> 

</Button> 

感謝

回答

1

你控制的Template設置爲空的模板(模板只包含一個觸發器)。這是凌駕現有的顯示邏輯。嘗試在Style Triger中設置觸發器。另外,如果你所做的只是改變控件的外觀,比每個控件的子類更好(更容易)的方法是僅爲現有控件提供一組Xaml樣式。通過這種方式,您可以使用可重用的ResourceDictionary,它可以應用於任何現有的應用程序,並且您的Xaml不必使用非標準控件的負載。您也可以選擇將其打包爲單獨的Xaml文件,或將其放入可重複使用的單獨程序集中。

+0

當然。這對我來說很糟糕。到目前爲止,我剛剛複製了我的App.xaml,出於某種原因,我從來沒有想過要使用ResourceDictionary。 * HeadDesk * –