WPF Custom Control based on TabItem and Themes
I have a Generic.xaml theme created that sets styles for all common controls, including TabItem This them is applying ok to all controls in the application
When I create a TabItem control and display it, it gets the Theme OK.
TabItem 开发者_开发技巧t = new TabItem();
t.Header = "Normal";
MainContentControl.Items.Add(t);
However when i create a Custom Control based on TabItem
public partial class ClosableTab : TabItem
and display it
ProActive.LocalControls.ClosableTab ct = new ProActive.LocalControls.ClosableTab();
ct.Header = "COMP";
MainContentControl.Items.Add(ct);
The theme is ignored.
I have tried over writing its defaultstyle using
DefaultStyleKeyProperty.OverrideMetadata(typeof(ClosableTab), new FrameworkPropertyMetadata(typeof(TabItem)));
Why if my custom control is based on a TabItem is the theme not also applying to it?
DefaultStyleKey is only used for looking up theme styles. Theme styles must be defined in the assembly defining the control or in a related assembly according to the ResourceDictionaryLocation specified in the control assembly. The TabItem themes are in PresentationFramework.Aero and WPF will look for ClosableTab themes in your assembly, so even if they have the same key it won't find them. Here is a good description of how WPF looks up theme styles.
Implicit styles will always be looked up using the actual type of the control, so if you have a <Style TargetType="TabItem">
in your resource dictionary then it won't affect a ClosableTab
.
The easiest way to have ClosableTab inherit the implicit style from TabItem is to create an implicit style for ClosableTab and use BasedOn:
<Style TargetType="local:ClosableTab" BasedOn="{StaticResource {x:Type TabItem}}" />
精彩评论