复制自定义控件的 WPF TabControl 选定 TabItem 设计时行为

发布于 2024-11-16 22:35:54 字数 448 浏览 3 评论 0原文

我正在开发一个显示内联弹出窗口的自定义控件,我想使用与 TabControl 采用的类似技术,以便仅在设计器中选择弹出窗口,或者更常见的是通过将光标放置在 XAML 中的弹出窗口声明中来选择弹出窗口它可以在设计器中直接可视化,而无需运行应用程序或手动更改任何运行时值。

我首先复制 TabControl 的实现,我已经成功地模仿了所有内容,但它都是从 Reflector 输出和 Stylesnooper 复制的。我重命名了所有控件部分,然后替换了默认模板,以便主控件使用 ItemsPresenter 而不是 ContentPresenter 来显示网格面板中相互叠加的各个弹出控件。到目前为止,这也效果很好。问题在于,我失去了让设计器遵循 XAML 编辑器中选择的项目的能力。

要么解释 TabControl 的设计时行为功能实际上如何工作来描述我上面描述的所选 TabItem 行为,要么只是提供一些关于如何实现我想要做的事情的指示,这都很好。

I'm developing a custom control which shows an inline popup window and I would like to use a similar technique as the TabControl employes so that only popup windows that are selected within the designer or more commonly by placing the cursor within the popup declaration in XAML that it is visualized right within the desiger without having to run the application or change any runtime values by hand.

I've started by duplicating the implementation of the TabControl which I have successfully mimicking everything but it is all copied from Reflector output and Stylesnooper. I've renamed all of the control parts and then replaced the default templates so that the main control uses an ItemsPresenter instead of a ContentPresenter to show the individual popup controls within a Grid panel overlayed on top of one another. So far this is working great too. The problem is that somewhere along the line I lost the ability to have the designer follow the item that is selected in the XAML editor.

Either an explanation of how the TabControl's design time behavior functionality actually works to describe the selected TabItem behavior that I described above or just some pointers on how one could achieve what I'm tryign to do would be great.

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

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

发布评论

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

评论(1

自控 2024-11-23 22:35:54

为了解决类似的问题,我必须为自定义选项卡控件创建设计时支持。以下是WPF 设计器扩展性。

基本上,我创建了一个 PrimarySelectionAdornerProvider 来处理点击交互,并创建了一个 FeatureConnector<> 。 /FeatureProvider 对用于选择更改(包括在 xaml 编辑器中进行的选择更改)。

功能提供程序/连接器:

[FeatureConnector(typeof(AutoTabPageSelectionFeatureConnector))]
class AutoTabPageSelectionFeatureProvider : FeatureProvider
{
    public AutoTabPageSelectionFeatureProvider()
        : base()
    {
        // sole purpose is to register the connector
    }

}
class AutoTabPageSelectionFeatureConnector : FeatureConnector<AutoTabPageSelectionFeatureProvider>
{
    public AutoTabPageSelectionFeatureConnector(FeatureManager manager)
        : base(manager)
    {
        SelectionOperations.Subscribe(this.Context, SelectionChanged);
    }

    private void SelectionChanged(Selection selection)
    {
        if (selection.PrimarySelection != null)
        {
            // navigate tree to find parent (custom tab page and custom tab control)
            for (ModelItem item = selection.PrimarySelection; item != null; item = item.Parent)
            {
                // once found, select appropriate tab
            }
        }
    }
}

编辑(更多信息):

Microsoft 链接 有许多指向演练的链接,应该会有所帮助。以下是入门的基本步骤:

  1. 创建一个新项目 MyAssembly.VisualStudio.Design.dll。

  2. 该库应编译到与 MyAssembly.dll 相同的位置(重要)。

  3. 添加对 Microsoft.Windows.Design.Extensibility 和 Microsoft.Windows.Design.Interaction 的引用。

  4. 添加对您的控件库的引用。

  5. 创建一个名为元数据的类

代码:

internal class Metadata : IProvideAttributeTable
{
    // Accessed by the designer to register any design-time metadata.
    public AttributeTable AttributeTable
    {
        get
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();

            // Add the adorner provider to the design-time metadata.
            builder.AddCustomAttributes(
                typeof(MyControl), // rename to your control's name
                new FeatureAttribute(typeof(MyPrimaryAdornerProvider)), // rename to whatever you will call your PrimaryAdornerProvider
                new FeatureAttribute(typeof(AutoTabPageSelectionFeatureProvider)) // rename to whatever you will call your SelectionFeatureProvider
                );

            return builder.CreateTable();
        }
    }
}
  1. 创建一个类来自 PrimarySelectionAdornerProvider(重命名为您想要的任何名称)。请参阅链接以获得详细的演练。

  2. 根据上面的示例创建 AutoTabPageSelectionFeatureProvider 和 AutoTabPageSelectionFeatureConnector。

To solve a similar problem, I had to create design time support for my custom tab control. Here is a link for WPF Designer Extensibility.

Basically, I created a PrimarySelectionAdornerProvider to handle click interaction and a FeatureConnector<> / FeatureProvider pair for selection changes (including selection changes made in the xaml editor).

The feature provider / connector:

[FeatureConnector(typeof(AutoTabPageSelectionFeatureConnector))]
class AutoTabPageSelectionFeatureProvider : FeatureProvider
{
    public AutoTabPageSelectionFeatureProvider()
        : base()
    {
        // sole purpose is to register the connector
    }

}
class AutoTabPageSelectionFeatureConnector : FeatureConnector<AutoTabPageSelectionFeatureProvider>
{
    public AutoTabPageSelectionFeatureConnector(FeatureManager manager)
        : base(manager)
    {
        SelectionOperations.Subscribe(this.Context, SelectionChanged);
    }

    private void SelectionChanged(Selection selection)
    {
        if (selection.PrimarySelection != null)
        {
            // navigate tree to find parent (custom tab page and custom tab control)
            for (ModelItem item = selection.PrimarySelection; item != null; item = item.Parent)
            {
                // once found, select appropriate tab
            }
        }
    }
}

Edit (more info):

This Microsoft link has a number of links to walk-throughs that should help. Here are the basic steps to get started:

  1. Create a new project, MyAssembly.VisualStudio.Design.dll.

  2. The library should compile to the same location as MyAssembly.dll (important).

  3. Add references to Microsoft.Windows.Design.Extensibility and Microsoft.Windows.Design.Interaction.

  4. Add a reference to your control library.

  5. Create a class called Metadata

Code:

internal class Metadata : IProvideAttributeTable
{
    // Accessed by the designer to register any design-time metadata.
    public AttributeTable AttributeTable
    {
        get
        {
            AttributeTableBuilder builder = new AttributeTableBuilder();

            // Add the adorner provider to the design-time metadata.
            builder.AddCustomAttributes(
                typeof(MyControl), // rename to your control's name
                new FeatureAttribute(typeof(MyPrimaryAdornerProvider)), // rename to whatever you will call your PrimaryAdornerProvider
                new FeatureAttribute(typeof(AutoTabPageSelectionFeatureProvider)) // rename to whatever you will call your SelectionFeatureProvider
                );

            return builder.CreateTable();
        }
    }
}
  1. Create a class MyPrimaryAdornerProvider from PrimarySelectionAdornerProvider (rename to whatever you want). See link for good walk-through.

  2. Create the AutoTabPageSelectionFeatureProvider and AutoTabPageSelectionFeatureConnector from the example above.

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