コントロールで定義されているリソースディクショナリを使用してコントロールのデフォルトスタイルを設定しようとしています。アプリケーション全体のスタイルにイベントハンドラを追加して、 。現在、イベントハンドラをスタイルに追加するとき(下のコードスニペットに表示)、スタイルはオーバーライドされます。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>
「I」デフォルトのスタイルオーバーライドからリソースがどこにあるかを指定する方法があると仮定しています。それは 'Themes/Generic.xaml'にあるはずです。 – Clemens