2009-08-05 42 views
3

我已经创建了一个自定义的WPF控件。该控件充当各个区域的容器(因此它可以像母版页一样工作)。在WPF中,如何给我的自定义控件设计模式中使用的默认样式?

此控件的样式由单独的资源字典在运行时加载如下:

<Application.Resources> 
     <ResourceDictionary> 
      <ResourceDictionary.MergedDictionaries> 
       <ResourceDictionary Source="/MyApp.Application;component/Themes/Theme.xaml" x:Name="theme"/> 
      </ResourceDictionary.MergedDictionaries> 
     </ResourceDictionary> 
    </Application.Resources> 

我的自定义控件的风格看起来如下...

<Style TargetType="{x:Type shareduc:EditControlMaster}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type shareduc:EditControlMaster}"> 
       <Grid> 
        <Grid.ColumnDefinitions></Grid.ColumnDefinitions> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="auto"></RowDefinition> 
         <RowDefinition Height="*"></RowDefinition> 
        </Grid.RowDefinitions> 

        <Border BorderBrush="{DynamicResource xxBorderBrush}" 
           BorderThickness="0,1,0,1" Background="White" Grid.Row="0"> 
         <Grid > 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="auto"></ColumnDefinition> 
           <ColumnDefinition Width="*"></ColumnDefinition> 
          </Grid.ColumnDefinitions> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="auto"></RowDefinition> 
           <RowDefinition Height="auto"></RowDefinition> 
          </Grid.RowDefinitions> 

          <ContentPresenter Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Margin="10" Content="{TemplateBinding Image}" /> 
          <ContentPresenter Grid.Row="0" Grid.Column="1" Margin="2" Content="{TemplateBinding Title}" /> 
          <ContentPresenter Grid.Row="1" Grid.Column="1" Margin="2" Content="{TemplateBinding Abstract}" /> 
         </Grid> 
        </Border> 

        <ContentPresenter Grid.Row="1" Margin="2" Content="{TemplateBinding Content}" /> 

       </Grid> 

      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

的问题是,这样式仅在运行时加载。所以在设计模式下,我的控件没有任何样式,也没有任何大小或布局。我怎样才能让我的控件成为设计模式的默认样式?

更新: 我取得一些进展...看来我可以指定一个默认的主题在一个名为\主题Generic.xaml使用。这在一个小样本项目中工作正常,但由于某种原因,当我在我的实际项目中做同样的事情时,我的VS2008设计师保持空白...帮助? :(

请注意,我的定制控件的代码如下:?

public partial class EditControlMaster : Control 
    { 
     static EditControlMaster() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(typeof(EditControlMaster), 
       new FrameworkPropertyMetadata(typeof(EditControlMaster))); 
     } 

     public object Title 
     { 
      get { return (object)GetValue(TitleProperty); } 
      set { SetValue(TitleProperty, value); } 
     } 

     public static readonly DependencyProperty TitleProperty = 
      DependencyProperty.Register("Title", typeof(object), 
      typeof(EditControlMaster), new UIPropertyMetadata()); 



     public object Image 
     { 
      get { return (object)GetValue(ImageProperty); } 
      set { SetValue(ImageProperty, value); } 
     } 

     public static readonly DependencyProperty ImageProperty = 
      DependencyProperty.Register("Image", typeof(object), 
      typeof(EditControlMaster), new UIPropertyMetadata()); 


     public object Abstract 
     { 
      get { return (object)GetValue(AbstractProperty); } 
      set { SetValue(AbstractProperty, value); } 
     } 

     public static readonly DependencyProperty AbstractProperty = 
      DependencyProperty.Register("Abstract", typeof(object), 
      typeof(EditControlMaster), new UIPropertyMetadata()); 

     public object Content 
     { 
      get { return (object)GetValue(ContentProperty); } 
      set { SetValue(ContentProperty, value); } 
     } 

     public static readonly DependencyProperty ContentProperty = 
      DependencyProperty.Register("Content", typeof(object), 
      typeof(EditControlMaster), new UIPropertyMetadata()); 
    } 

回答

1

你尝试

public EditControlMaster() 
{ 
    DefaultStyleKey = typeof(EditControlMaster); 
} 

作为构造的一部分

+0

嗨塞尔吉奥。我尝试了类似的东西(见上面的代码)。但是,这不能编译。也许我是从不同的基类继承而来的? – willem 2009-08-05 13:47:40

0

尝试的风格移动到一个标准的地方。

  • 添加/新我TEM /自定义控制(WPF)/ “MyDummyControl”
  • 现在把你的风格 “主题/ Generic.xaml” 已创建
  • 删除 “MyDummyControl” 文件和风格
  • 删除您Theme.xaml,并MergedDictionaries
5

通过大量围绕项目文件戳我已经想通了,什么是错的!

  1. Themes \ Generic.xaml包含您的控件的默认样式。这可以。
  2. 你Assembly.cs文件需要包含以下属性:

    [assembly: ThemeInfo(
        ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 
        //(used if a resource is not found in the page, 
        // or application resource dictionaries) 
        ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 
        //(used if a resource is not found in the page, 
        // app, or any theme specific resource dictionaries) 
    )] 
    

瞧! VS2008设计师的作品!

0

还有一两件事,在我的经验,在主题限定的风格,使用DynamicResource \ generic.xaml(像你这样的边界)不工作(至少,并不总是有效)。您应该考虑将其更改为StaticResource查找。

相关问题