2009-08-05 1 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

+0

私はそれがSilverLightのためだと思います –

0

を標準にスタイルを移動してみてください?場所。

  • 追加/新規TEM /カスタムコントロール(WPF)/ "MyDummyControl"
  • は今 "テーマ/ Generic.xaml" であなたのスタイルを置く
  • 削除 "MyDummyControl" ファイルとスタイルを作成しました
  • あなたTheme.xamlを削除し、MergedDictionaries
5

私は何が間違っているのか分かりました。

  1. テーマ\ Generic.xamlには、コントロールのデフォルトのスタイルが含まれています。これは問題ありません。

    [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) 
    )] 
    

出来上がり:

  • あなたAssembly.csファイルには、次の属性が含まれている必要があり! VS2008デザイナーの作品!

  • 0

    私の経験上、もう1つ、テーマに定義されているスタイルでDynamicResourceを使用しています(あなたがBorderの場合のように)動作しません(少なくとも、常に動作しません)。それをStaticResourceルックアップに変更することを検討する必要があります。

    関連する問題