wpf 如何在WPF的选项卡控件中添加用户控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36467201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:44:53  来源:igfitidea点击:

How to add user control in tab control in WPF

c#wpftabitem

提问by Elahe

The below article shows how to create dynamic tabs in WPF, that in each tab it will add just one text box.

下面的文章展示了如何在 WPF 中创建动态选项卡,在每个选项卡中它只会添加一个文本框。

private TabItem AddTabItem()
{
    int count = _tabItems.Count;

    // create new tab item
    TabItem tab = new TabItem();

    tab.Header = string.Format("Tab {0}", count);
    tab.Name = string.Format("tab{0}", count);
    tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;

    tab.MouseDoubleClick += new MouseButtonEventHandler(tab_MouseDoubleClick);

    // add controls to tab item, this case I added just a textbox
    TextBox txt = new TextBox();

    txt.Name = "txt";
    tab.Content = txt;
    // insert tab item right before the last (+) tab item
    _tabItems.Insert(count - 1, tab);

    return tab;
}

http://www.codeproject.com/Articles/493538/Add-Remove-Tabs-Dynamically-in-WPF

http://www.codeproject.com/Articles/493538/Add-Remove-Tabs-Dynamically-in-WPF

what can I do for adding some complex controls that their positions are fixed instead of just 1 text box? can I create a user control for this purpose? so how can I add the user control to tab control?

我可以做什么来添加一些复杂的控件,它们的位置是固定的,而不仅仅是 1 个文本框?我可以为此目的创建用户控件吗?那么如何将用户控件添加到选项卡控件?

回答by Noam M

Try the next steps:

尝试以下步骤:

  1. Add a user control (Lets say in ComplexControl.xaml)

    <UserControl ... >
       <Grid>
           <Rectangle Width="100" Height="100" Fill="Red"/>
        </Grid>
     </UserControl> 
    
  2. Create a class

    Public myComplexContolClass
    {
         //....
    }
    
  3. Map them together so when you have a myComplexContolClass in your app visually it will be the UserControl from 1. The map can be done with DataTemplate:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        ...
                        xmlns:models="clr-namespace: ... .Model"
                        xmlns:views="clr-namespace: ... .View"
                        >
    
        <DataTemplate DataType="{x:Type models:myComplexContolClass}">
            <views:ComplexControl/>
        </DataTemplate>
    
    </ResourceDictionary>
    
  1. 添加用户控件(让我们在 ComplexControl.xaml 中说)

    <UserControl ... >
       <Grid>
           <Rectangle Width="100" Height="100" Fill="Red"/>
        </Grid>
     </UserControl> 
    
  2. 创建一个班级

    Public myComplexContolClass
    {
         //....
    }
    
  3. 将它们映射在一起,这样当你的应用程序中有一个 myComplexContolClass 时,它就会是 1 中的 UserControl。可以使用 DataTemplate 完成映射:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        ...
                        xmlns:models="clr-namespace: ... .Model"
                        xmlns:views="clr-namespace: ... .View"
                        >
    
        <DataTemplate DataType="{x:Type models:myComplexContolClass}">
            <views:ComplexControl/>
        </DataTemplate>
    
    </ResourceDictionary>
    

Or

或者

    <Window ...
            xmlns:models="clr-namespace: ... .Model"
            xmlns:views="clr-namespace: ... .View"
            >

        <Window.Resources>

            <DataTemplate DataType="{x:Type models:myComplexContolClass}">
                <views:ComplexControl/>
            </DataTemplate>

        </Window.Resources>

        // ...

    </Window>
  1. Add it to your code:

    private TabItem AddTabItem()
    {
        // ...
    
        myComplexContolClass control = new myComplexContolClass();
        tab.Content = control;
    
        // ...
    }
    
  1. 将其添加到您的代码中:

    private TabItem AddTabItem()
    {
        // ...
    
        myComplexContolClass control = new myComplexContolClass();
        tab.Content = control;
    
        // ...
    }