I'm trying to set the default style of a control using a resource dictionary defined in the control so i can added event handlers to the style throughout the application to change colors of the background based on bindings. Currently when adding event handlers to the style (seen in bottom code snippet) the style is overridden.
I'm happy with how the control looks so i have removed the code from the style to make it more readable.
The problem is when adding triggers to the bubblelist
the default style is overridden seen in last code snippet.
code behind the control:
public partial class BubbleList : ListBox
{
public BubbleList()
{
InitializeComponent();
}
static BubbleList()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BubbleList),
new FrameworkPropertyMetadata(typeof(BubbleList)));
}
}
Found from here: https://stackoverflow.com/a/28715964/3603938
How the style is set for the control:
<ListBox ... >
<ListBox.Resources>
<Style TargetType="{x:Type local:BubbleList}"
BasedOn="{StaticResource {x:Type ListBox}}">
....
</Style>
</ListBox.Resources>
</ListBox>
Desired 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>
Complete Solution
Based on Clemens solution i've opted to used the themes/generic.xaml resource dictionary to contain the styles for the controls.
BubbleList class which overrides default ListBoxItem with 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 with dependencies removed
public class BubbleListItem : ListBoxItem
{
...
static BubbleListItem()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BubbleListItem),
new FrameworkPropertyMetadata(typeof(BubbleListItem)));
}
...
}
Styles for both in liststyles.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>
Themes/generic.xaml
<ResourceDictionary ... >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="liststyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Usage:
<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>
答案 0 :(得分:1)
While the default Style of a control is located in a special ResourceDictionary in Themes/Generic.xaml
, you can easily extend it when you use the control:
<lists:BubbleList ...>
<lists:BubbleList.Style>
<Style TargetType="lists:BubbleList"
BasedOn="{StaticResource {x:Type lists:BubbleList}}">
...
</Style>
</lists:BubbleList.Style>
</lists:BubbleList ...>