简体   繁体   English

WPF Custom TabItem - 控件未显示在Visual Studio Designer中

[英]WPF Custom TabItem - Controls not displayed in Visual Studio Designer

I created a custom TabItem with a DockPanel and a Button in it. 我创建了一个带有DockPanel和Button的自定义TabItem。

XAML: XAML:

<TabItem
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      mc:Ignorable="d" x:Class="MovieDB.UI.UserControlls.SearchTab" d:DesignWidth="500.038" d:DesignHeight="309.055">

    <DockPanel Background="#FFE5E5E5">
        <Button x:Name="button" Content="Button" Height="100" Width="75" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </DockPanel>
</TabItem>

C#: C#:

namespace MovieDB.UI.UserControlls
{
    /// <summary>
    /// Interaktionslogik für SearchTab.xaml
    /// </summary>
    public partial class SearchTab : TabItem
    {
        private SearchContainer<SearchMovie> results;

        public SearchTab()
        {
            InitializeComponent();
            this.Header = "Suche";
        }

        public SearchTab(SearchContainer<SearchMovie> results):this()
        {
            this.updateSearch(results);
        }

        public void updateSearch(SearchContainer<SearchMovie> results)
        {
            clear();
            if(results.TotalResults == 0)
            {

            }
            else
            {
                this.results = results;
                Debug.WriteLine("Results: " + results.Results.Count());
            }
        }

        private void clear()
        {

        }
    }
}

If launch my program the button is displayed (Screenshot 2). 如果启动我的程序,则显示按钮(屏幕截图2)。 But the button and i guess the panel itselve does noch show up in the Visual Studio 2015 Designer (Screenshot 1). 但是按钮和我想面板itselve确实noch出现在Visual Studio 2015 Designer中(截图1)。

Where is the Problem? 问题出在哪儿?

截图设计师 运行Programm的截图

If you put the TabItem in a TabControl it works fine.I think that's the Problem. 如果你把TabItem放在一个TabControl中它工作正常。我认为这是问题。 The designer needs a navigation control like TabControl,Window Page etc. as root. 设计师需要像TabControl,Window Page等导航控件作为root。

<TabControl x:Class="CustomControls.tab"
             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"
            mc:Ignorable="d" 
            d:DesignWidth="500.038" d:DesignHeight="309.055">
<TabItem>
    <DockPanel Background="#FFE5E5E5">
        <Button x:Name="button" Content="Button" Height="100" Width="75" HorizontalAlignment="Left" VerticalAlignment="Top"/>
    </DockPanel>
    </TabItem>
</TabControl>

What you did should work. 你做了什么应该工作。 But it doesn't, and I'll later try to show why. 但事实并非如此,我稍后会尝试说明原因。

But first, my workaround: 但首先,我的解决方法:

  • open designer, and right-click on it, 打开设计器,右键单击它,
  • select: Edit Additional Templates , then Edit Generated Content , then Create Empty... Give your new DataTemplate a name, say ' DataTemplate1 ' 选择: 编辑其他模板 ,然后编辑生成的内容 ,然后创建空...给新的DataTemplate命名,说' DataTemplate1 '

Designer will generate some code - an empty DataTemplate with a key DataTemplate1 , and it will add your to TabItem the ContentTemplate attribute, with DataTemplate1 assigned to it (by DynamicResource ). Designer将生成一些代码 - 一个带有关键DataTemplate1的空DataTemplate ,它将向TabItem添加ContentTemplate属性,并为其分配DataTemplate1 (通过DynamicResource )。

Now move the content of your TabItem to the DataTemplate1 . 现在将TabItem的内容移动到DataTemplate1 The designer should correctly display your layout. 设计师应该正确显示您的布局。

If you close and reopen your SearchTab control, you'll find that you can't see the content again. 如果您关闭并重新打开SearchTab控件,您将发现无法再次看到该内容。 To have it back right click on the designer, select Edit Additional Template , then Edit Generated Content , then Edit Current . 要右键单击设计器,请选择编辑其他模板 ,然后选择编辑生成的内容 ,然后选择编辑当前

Why the designer couldn't load your SearchTab ? 为什么设计师无法加载您的SearchTab If you right-click on the designer and select Edit Template , then Edit a Copy... you'll have TabItem style generated. 如果右键单击设计器并选择“ 编辑模板” ,然后选择“ 编辑 副本...” ,则会生成TabItem样式。 The header of the TabItem is displayed by the ContentPresenter, while the TabItem's content must by displayed inside the innerBorder Border . TabItem的标题由ContentPresenter显示,而TabItem的内容必须显示在innerBorder Border This border has Opacity set to zero, and it's being changed to 1 by MultiTriggers , which are dependent on the TabControl . 此边框的“ Opacity设置为零,并且MultiTriggers其更改为1,这取决于TabControl The TabControl in your case doesn't exist, so all the bindings are broken. 您的案例中的TabControl不存在,因此所有绑定都被破坏。 Which suggests, that changing a default Style for 'TabItem', would also solve your problem. 这表明,更改“TabItem”的默认Style也可以解决您的问题。

Although I do not have my dev machine to work with, I can only offer a suggestion. 虽然我没有使用我的开发机器,但我只能提供一个建议。 For whatever you want on your tab item control, why not put all your controls in a custom user control or grid with the premise that you KNOW you will be adding it to tab control that is dynamically added to your tab page. 对于您在标签项控件上想要的任何内容,为什么不将所有控件放在自定义用户控件或网格中,前提是您知道要将其添加到动态添加到标签页的标签控件中。

The tab item needs its proper parent tab control to render it in the designer. 选项卡项需要其正确的父选项卡控件才能在设计器中呈现它。 But if you build everything as a standard control make it look and operate as you expect. 但是,如果您将所有内容构建为标准控件,请使其外观和操作符合您的预期。

Then at run-time, add your tab item, then add the custom control to your dynamically added tab item and see if that works for you. 然后在运行时添加标签项,然后将自定义控件添加到动态添加的标签项中,看看它是否适合您。 You apparently have all the other components / processes working. 您显然拥有所有其他组件/流程。 Even if your sample control was just a label, textbox or something similar. 即使您的样本控件只是标签,文本框或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM