2012-10-15 27 views
1
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
       xmlns:local="clr-namespace:CuratioCMS.Client.UI.Controls"> 
<Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type local:ImageButton}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:ImageButton}"> 
       <StackPanel Height="Auto" Orientation="Horizontal"> 
        <Image Width="20" 
          Height="20" 
          Margin="10,4,0,4" 
          Source="{Binding Path=Image, 
              RelativeSource={RelativeSource TemplatedParent}}" 
          Stretch="Fill" /> 
        <TextBlock Margin="5,0,10,0" 
           HorizontalAlignment="Left" 
           VerticalAlignment="Center" 
           FontSize="12" 
           FontWeight="Bold" 
           Foreground="{TemplateBinding Foreground}" 
           Text="{TemplateBinding Label}" /> 
       </StackPanel> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

不能在定製控件模板

這裏面Generic.xaml代碼爲ImageButton的自定義控件找到資源。白衣按預期工作,但我不能從基本按鈕陳舊等等,而不是繼承了按鈕的所有基本的造型繼承了這種風格不作任何基本樣式創建自定義的控制,並儘可能

BasedOn="{StaticResource {x:Type Button}}" 

內VS與行顯示錯誤,說資源「{X:類型System.Windows.Controls.Button}」未發現

我不知道如何實現期望的造型,爲什麼這個錯誤消息顯示有Visual Studio編輯器裏面了

回答

0

您正在基於通用按鈕樣式定義樣式(它位於Generic.xaml內),然後重新定義樣式的模板。這樣基礎樣式將被覆蓋。

,如果你想改變風格了一下,你有兩個選擇:

選項1:

  1. 提供通用按鈕樣式的按鍵(找到TargetType="{x:Type Button}"內Generic.xaml定義的風格並添加x:Key="someName"其屬性)
  2. 將其他所有按鈕(除ImageButtons)的風格{StaticResource someName}
  3. 設置你的ImageButton樣式支持算法FMP屬性{StaticResource someName}

選項2:

  1. 你可以通過設置它的TargetType到的ImageButton和「不是」設置它
  2. 變化的任何鍵使通用按鈕樣式的副本
  3. 離開它一般它的所需部分。

選項2通常是更好的解決方案,因爲它不需要大量更改,但是我前面已經完成了你想要做的事情,而且我沒有找到任何避免重複樣式代碼的方法。

+0

你能解釋一下嗎?我同意重寫控制是我在這裏做的,那麼在命名默認樣式下你是什麼意思?這不是一個用戶控件它是自定義控件,所以我認爲你明白這些是不同的情況 –

+0

我對我以前的答案中的一些錯誤表示歉意,我編輯它並添加更多解釋。 – Bijan

+0

好吧,但第二個選項,它會問每個主題相關的信息,我將不得不添加參考航空DLL和定義Luna和其他Windows主題的主題以及這不是我真正想要的 –