开发者

WPF custom TabItem disappears upon restyle?

开发者 https://www.devze.com 2023-03-06 13:41 出处:网络
I have a TabControl with a custom TabItem. The view xaml looks like this. <UserControl x:Class=\"PeripheryModule.Views.MainTabView\"

I have a TabControl with a custom TabItem. The view xaml looks like this.

<UserControl x:Class="PeripheryModule.Views.MainTabView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:PeripheryModule.Controls"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <TabControl>

            <TabItem Header="TabItem 4" />
            <local:CustomTabItem Header="Testing" />

        </TabControl>
    </Grid>
</UserControl>

the CustomTabItem.xaml file looks like this

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:PeripheryModule.Controls">

    <Style TargetType="{x:Type local:CustomTabItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:CustomTabItem}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</ResourceDictionary>

and the CustomTabItem.xaml.cs file looks like this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PeripheryModule.Controls
{
    public class CustomTabItem : TabItem
    {
        static CustomTabItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomTabItem), new FrameworkPropertyMetadata(typeof(CustomTabItem)));
        }
    }
}

I get no errors thrown, all that happens is the tabItem simply doesn't show up. It will, however, show up if i comment out the DefaultStyleKeyProperty line in CustomTabItem.aspx.cs.

I've p开发者_JS百科robably got this all set up wrong anyways. Ultimate goal is to have closeable tabs.


Excerpt:

First, if you are not familiar with the concept of theme style vs explicit style I suggest you read this blog http://www.interact-sw.co.uk/iangblog/2007/02/14/wpfdefaulttemplate. All controls need a theme style (which is usually defined in files named Aero.NormalColor.xaml/Generic.xaml/etc under a folder named Themes) which defines their default look (Template)/properties and an optional explicit style (which is defined at the element/window/app level either with implicit key or explicit key).

DefaultStyleKeyProperty defines the key used to find the theme style of the control. If you comment out the line, you will end up with the default theme style of the base class. As a quick test, change the base class of custom control to Button. If you comment out the line, your custom control will get the theme style of Button and it will look like a button. If you dont comment out the line, your custom control will get the default style defined in generic.xaml.

From: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9df46c62-3715-4e16-8ef6-538068c28eb6/

Edit:

Is there any reason why you don't want to define the header template and assign it?

ex. <TabItem Header="Testing" HeaderTemplate="{StaticResource customHeaderTemplate}" />


As Schalk points out, the DefaultStyleKeyProperty identifies the key to the default Style, which must be contained in a Resource dictionary under a Themes folder. So if you move/rename your CustomTabItem.xaml to be under Themes\Generic.xaml, then it should load and be applied.

You can also create different versions of your styles based on the system themes, by adding files named AeroNormalColor.xaml, Classic.xaml, etc to the Themes folder. These files would need to have copies of your Style also, presumably with visual tweaks.

You can download the default Styles for the native controls from here. This would allow you to copy the default Style for TabItem and tweak it to your needs.

Or you could do something like in this question, where the close button is added to the HeaderTemplate.

0

精彩评论

暂无评论...
验证码 换一张
取 消