2017-07-12 21 views
0

コントロールで定義されているリソースディクショナリを使用してコントロールのデフォルトスタイルを設定しようとしています。アプリケーション全体のスタイルにイベントハンドラを追加して、 。現在、イベントハンドラをスタイルに追加するとき(下のコードスニペットに表示)、スタイルはオーバーライドされます。XAML ListBoxとListBoxItemコントロールを拡張する

コントロールがどのように表示されているかに満足していますので、より読みやすくするためにスタイルからコードを削除しました。

bubblelistにトリガーを追加すると、デフォルトのスタイルが最後のコードスニペットで上書きされるという問題があります。

制御の背後にあるコード:ここからが見つかり

public partial class BubbleList : ListBox 
{ 
    public BubbleList() 
    { 
     InitializeComponent(); 
    } 

    static BubbleList() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(BubbleList), 
      new FrameworkPropertyMetadata(typeof(BubbleList))); 
    } 
} 

<ListBox ... > 

    <ListBox.Resources> 
     <Style TargetType="{x:Type local:BubbleList}" 
       BasedOn="{StaticResource {x:Type ListBox}}">  
      .... 
     </Style> 
    </ListBox.Resources> 
</ListBox> 

希望usuage:

<lists:BubbleList ...> 
    <lists:BubbleList.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Style.Triggers> 
       ... 
      </Style.Triggers> 
     </Style> 
    </lists:BubbleList.ItemContainerStyle> 
    <lists:BubbleList.ItemTemplate> 
     <DataTemplate> 
      <Label ... /> 
     </DataTemplate> 
    </lists:BubbleList.ItemTemplate> 
</lists:BubbleList> 
スタイルが制御用に設定されている方法 https://stackoverflow.com/a/28715964/3603938

Complete Solution

Clemensのソリューションに基づいて、themes/generic.xamlリソースディクショナリを使用してコントロールのスタイルを格納することを選択しました。

<ResourceDictionary ... > 
    <Style TargetType="{x:Type local:BubbleListItem}" 
      BasedOn="{StaticResource {x:Type ListBoxItem}}"> 
     ... 
    </Style> 

    <Style TargetType="{x:Type local:BubbleList}" 
      BasedOn="{StaticResource {x:Type ListBox}}"> 
     ... 
    </Style> 
</ResourceDictionary> 

テーマ/ generic.xaml:依存関係を持つBubbleListItem

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

    protected override bool IsItemItsOwnContainerOverride(object item) 
    { 
     return item is BubbleListItem; 
    } 

    protected override DependencyObject GetContainerForItemOverride() 
    { 
     return new BubbleListItem(); 
    } 
} 

BubbleListItemでデフォルトListBoxItemを上書きします

BubbleListクラスはliststyles.xamlの両方のための

public class BubbleListItem : ListBoxItem 
{ 
    ... 

    static BubbleListItem() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(BubbleListItem), 
      new FrameworkPropertyMetadata(typeof(BubbleListItem))); 
    } 

    ... 
} 

スタイルを削除しました

<ResourceDictionary ... > 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="liststyles.xaml" /> 
    </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

使用方法:コントロールのデフォルトのスタイルがThemes/Generic.xamlに特別のResourceDictionaryに位置していますが、あなたがコントロールを使用するとき

<lists:BubbleList ... > 
    <lists:BubbleList.ItemContainerStyle> 
     <Style TargetType="{x:Type lists:BubbleListItem}" 
       BasedOn="{StaticResource {x:Type lists:BubbleListItem}}"> 
      <Style.Triggers> 
       ... 
      </Style.Triggers> 
     </Style> 

    </lists:BubbleList.ItemContainerStyle> 
    <lists:BubbleList.ItemTemplate> 
     <DataTemplate> 
      <Label ... /> 
     </DataTemplate> 
    </lists:BubbleList.ItemTemplate> 
</lists:BubbleList> 
+1

「I」デフォルトのスタイルオーバーライドからリソースがどこにあるかを指定する方法があると仮定しています。それは 'Themes/Generic.xaml'にあるはずです。 – Clemens

答えて

1

、あなたは簡単にそれを拡張することができます

<lists:BubbleList ...> 
    <lists:BubbleList.Style> 
     <Style TargetType="lists:BubbleList" 
       BasedOn="{StaticResource {x:Type lists:BubbleList}}"> 
      ... 
     </Style> 
    </lists:BubbleList.Style> 
</lists:BubbleList ...> 
+0

私はResourceDictionary 'Themes/Generic.xaml'を使わないように言われていますが、これは必要なことをする唯一の方法だと思われます。私は他の人を助けるための完全な解決策で質問を更新します。あなたの答えを親切にありがとう:) –

関連する問題