WPF 自定义 TabItem 在重新样式后消失?

发布于 2024-11-08 03:30:54 字数 2691 浏览 1 评论 0原文

我有一个带有自定义 TabItem 的 TabControl。视图 xaml 看起来像这样。

<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>

CustomTabItem.xaml 文件看起来像这样

<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>

, CustomTabItem.xaml.cs 文件看起来像这样

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)));
        }
    }
}

我没有抛出任何错误,所发生的只是 tabItem 根本不显示。但是,如果我注释掉 CustomTabItem.aspx.cs 中的 DefaultStyleKeyProperty 行,它就会显示。

无论如何,我可能把这一切都设置错了。最终目标是拥有可关闭的选项卡。

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 probably got this all set up wrong anyways. Ultimate goal is to have closeable tabs.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事和酒 2024-11-15 03:30:54

摘抄:

首先,如果您不熟悉主题风格与显式风格的概念,我建议您阅读此博客http://www.interact-sw.co.uk/iangblog/2007/02/14/wpfdefaulttemplate。所有控件都需要一个主题样式(通常在名为 Themes 的文件夹下名为 Aero.NormalColor.xaml/Generic.xaml/etc 的文件中定义),该样式定义其默认外观(模板)/属性和可选的显式样式(定义为在元素/窗口/应用程序级别使用隐式键或显式键)。

DefaultStyleKeyProperty 定义用于查找控件主题样式的键。如果注释掉该行,您最终将得到基类的默认主题样式。作为快速测试,将自定义控件的基类更改为 Button。如果您注释掉该行,您的自定义控件将获得 Button 的主题样式,并且它看起来像一个按钮。如果您不注释掉该行,您的自定义控件将获得 generic.xaml 中定义的默认样式。

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

编辑:

有什么理由不这样做吗想要定义标题模板并分配它?

前任。

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}" />

红衣飘飘貌似仙 2024-11-15 03:30:54

正如 Schalk 指出的,DefaultStyleKeyProperty 标识默认样式的键,该键必须包含在 Themes 文件夹下的资源字典中。因此,如果您将 CustomTabItem.xaml 移动/重命名到 Themes\Generic.xaml 下,那么它应该加载并应用。

您还可以通过将名为 AeroNormalColor.xaml、Classic.xaml 等的文件添加到 Themes 文件夹中,根据系统主题创建不同版本的样式。这些文件还需要有您的样式的副本,大概需要进行视觉调整。

您可以从 这里。这将允许您复制 TabItem 的默认样式并根据您的需要进行调整。

或者你可以这样做 问题,其中关闭按钮被添加到 HeaderTemplate 中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文