2011-02-10 69 views
1

所以我喜歡自定義控件的外觀可以在generic.xaml中重新定義的事實,所以我想創建一個。但我的問題是,您最初如何設計(使用設計師)自定義控件的外觀?與用戶控件不同,它沒有附加的xaml/designer窗口。那麼必須在代碼中設計它們嗎?WPF:如何在設計器中定義自定義控件的外觀?

+0

Expression Blend爲您提供了一個設計界面... – 2011-02-10 16:57:56

回答

2

您創建一個ResourceDictionaryXAML文件,在其中您控制定義CustomTemplate/Style,然後合併所有這些字典在Generic.xaml字典。

例如,定義一個控件的樣式:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style TargetType="{x:Type Button}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type Button}"> 
        <Grid x:Name="RootElement" > 
         <ContentPresenter 
          Content="{TemplateBinding Content}" 
          ContentTemplate="{TemplateBinding ContentTemplate}" 
          Foreground="{TemplateBinding Foreground}" 
          VerticalAlignment="Center" HorizontalAlignment="Center"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</ResourceDictionary> 

而在你Generic.xaml

<ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="MyButtonStyle.xaml"/> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

的結果應該是Button類型的控件現在指定的樣式呈現,即使在IDE設計師。

+0

這不會回答我的問題 – foreyez 2011-02-10 17:02:15