2016-05-30 3 views
3

カスタムユーザーコントロールを作成しようとしています。カスタムコントロールのスタイルを設定します

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" 
    xmlns:themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
    xmlns:components="clr-namespace:ORPO.WPF.Components"> 

    <Style TargetType="{x:Type components:HeaderFilterDataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}"> 
     ... 
    </Style> 
    <Style TargetType="{x:Type DataGridColumnHeader}"> 
     ... 
    </Style> 
</ResourceDictionary> 

と私のカスタムコントロールクラス:私は2つのスタイルでのResourceDictionaryファイルを(テーマGeneric.xamlを\)作成したそれは正常に動作します

public class HeaderFilterDataGrid : DataGrid 
    { 
... 
static HeaderFilterDataGrid() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderFilterDataGrid), 
new FrameworkPropertyMetadata(typeof(HeaderFilterDataGrid)));    
     } 
... 
} 

私が適用されます最初のスタイル

DefaultStyleKeyProperty.OverrideMetadata(typeof(HeaderFilterDataGrid), 
    new FrameworkPropertyMetadata(typeof(HeaderFilterDataGrid))); 

カスタムコントロールに2番目のスタイルを適用するにはどうすればよいですか? 両方のスタイルを同時に適用する必要があります。

+2

'DataGridColumnHeader'スタイルを' HeaderFilterDataGrid'スタイルの 'Style.Resources'に配置しようとしてください。 – Clemens

+0

それは動作します!ありがとうございました! – Stopee

答えて

3

DataGridColumnHeaderのスタイルをHeaderFilterDataGridスタイルのリソースに入れます。このようにしてDataGridColumnHeaderは、HeaderFilterDataGrid内のすべてのDataGridColumnHeadersのデフォルトスタイルになります。

<ResourceDictionary ...> 
    <Style TargetType="{x:Type components:HeaderFilterDataGrid}" 
      BasedOn="{StaticResource {x:Type DataGrid}}"> 
     <Style.Resources> 
      <Style TargetType="{x:Type DataGridColumnHeader}"> 
      ... 
      </Style> 
     </Style.Resources> 
     ... 
    </Style> 
</ResourceDictionary> 
関連する問題