C#WPF选项卡tabcontrol定制 [英] C# WPF tabs tabcontrol customisation

查看:460
本文介绍了C#WPF选项卡tabcontrol定制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我目前有一个设置,允许我添加和删除新标签页,唯一的问题是我在向此新标签页添加任何内容时遇到了问题.我想在其中有一个文本框,但在添加一个文本框时遇到麻烦,选项卡只是显示为空白.任何帮助都会很棒,谢谢!

这是我的设置方法:

构成新标签的按钮:

Heys, I currently have a set up that allows me to add and remove new tabs, the only problem is I have problem adding anything to this new tab. I want to have a text box in there but am having troubling adding one, the tabs are just appearing as blank. Any help would be great, thank you!

Here is how I have it setup:

The button that makes a new tab:

<Button
                               FontSize="12"
                               Content="Private Chat"
                               Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="147,118,0,0" VerticalAlignment="Top" Width="75"
                               Name="btnPrivateChat" Click="btnPrivateChat_Click" />




调用此函数的地方:




Which calls this function:

private void btnPrivateChat_Click(object sender, RoutedEventArgs e)
       {

           ListBoxItem userName = lstOnlineUsers.SelectedItem as ListBoxItem;

           if (userName != null)
           {
               TabHandler newTabItem = new TabHandler();
               newTabItem.Title = userName.Content.ToString();
               ;
               ChatTabControl.Items.Add(newTabItem);
               newTabItem.Focus();
               newTabItem.ApplyTemplate();
           }
           else
           {

               MessageBox.Show("Error! Please select a username.");
           }
       }



然后创建用户控件TabHandler的实例:



Which then creates an instance of the usercontrol TabHandler:

public TabHandler()
{
    // Create an instance of the usercontrol
    TabHeader tabHeader = new TabHeader();








用户控件TabHandler(XAML):








Usercontrol TabHandler (XAML) :

<usercontrol x:class="TrapezoidTabs.TabHeader" xmlns:x="#unknown">
             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:DesignHeight="23" d:DesignWidth="81" Margin="0">
    <grid>
        <Button Content="X"  Height="19" HorizontalAlignment="Right" Margin="0,3,4,0" Name="btnCloseTab" VerticalAlignment="Top" Width="20" FontFamily="Courier" FontWeight="Bold" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" FontStretch="Normal" Visibility="Visible" FontSize="14" Padding="0" ToolTip="Close"/>
        <Label FontSize="14" Foreground="Black" Content="User"  Height="23" HorizontalAlignment="Left" Margin="4,1,0,0" Name="lblTabTitle" VerticalAlignment="Top" />
    </grid>
</usercontrol>







我应该制作一个新的窗口而不是一个用户控件吗?我真的想向每个页面添加内容.在此先谢谢您!







Should I be making a new Window instead of a usercontrol? I realy want to add content to each page. Thanks in advance!

推荐答案

尝试使用TabItem的Content属性.

newTabItem.Content = [您的文本框]
Try the Content property of your TabItem.

newTabItem.Content = [Your Textbox]


我对您的工作感到困惑,但是我有一个建议.

可以将ItemSource绑定到DataContext内部的集合,而不是添加到TabControl.如果该集合是ObservableCollection,它将为您更新.

接下来,该集合可以保存其他用户控件的数据上下文.然后,您可以为该数据上下文设置数据模板

例如
I am little confused by what you are doing but I have a suggestion.

Rather than adding to the TabControl you can bind the ItemSource to a collection inside the DataContext. If the collection is an ObservableCollection it will update for you.

Next, the collection can hold data context for other user controls. You then set the data template for that datacontext

e.g.
<datatemplate datatype="{x:Type viewModels:YourDataContextObject}">
   <view:yourviewcontrol xmlns:view="#unknown" />
</datatemplate>



请取消保护xmlns:view =#unknown".一些格式问题.



Please disreguard the xmlns:view="#unknown". Some formatting issues.


这篇关于C#WPF选项卡tabcontrol定制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆