2009-08-05 43 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查找。

相關問題