2011-02-10 67 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