如何将TextBox添加到TabItem?

时间:2013-01-16 17:06:18

标签: c# wpf

每次点击按钮时,我都会尝试向TabItem添加新TabControl,我对此没有任何问题。但我希望每个TabItem内都有一个文本框。我怎么做?我需要用我想的代码来做到这一点。

TabItem newTab = new TabItem();
                newTab.Header = ncn.courseName;
                newTab.FontSize = 20;
                TextBox textbox = new TextBox();
                textbox.Width = 200;
                textbox.Height = 100;
                textbox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                textbox.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                Grid grid = new Grid();
                grid.Children.Add(textbox);
                newTab.Content = grid;
                this.Courses.Items.Add(newTab);
                this.Courses.SelectedItem = newTab;

1 个答案:

答案 0 :(得分:1)

如果您只想使用代码而不使用MVVM模式,可以通过这种方式解决:

private void button1_Click(object sender, RoutedEventArgs e)
{
    TabItem item = null;
    Grid grid = null;
    TextBox textbox = null;

    try
    {
        // Creating the TextBox
        textbox = new TextBox();
        textbox.Width = 200;
        textbox.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
        textbox.VerticalAlignment = System.Windows.VerticalAlignment.Top;

        // Creating the Grid (create Canvas or StackPanel or other panel here)
        grid = new Grid();
        grid.Children.Add(textbox);     // Add more controls

        item = new TabItem();
        item.Header = "Hello, this is the new tab item!";
        item.Content = grid;            // OR : Add a UserControl containing all controls you like, OR use a ContentTemplate

        MyTabControl.Items.Add(item);
        MyTabControl.SelectedItem = item;   // Setting focus to the new TabItem
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error creating the TabItem content! " + ex.Message);
    }
    finally
    {
        textbox = null;
        grid = null;
        item = null;
    }
}

通过使用代码隐藏来解决它“旧方式”。

如果你在另一个想要像它应该使用的WPF,你可以这样做。 为了简化一点,我使用代码隐藏作为DataContext。我建议在运行代码中使用类。 如果使用Button Command,我也使用了Cutton click事件。

首先,我为标签项创建一个“holder”类,保存您需要的数据。

<强> TabItemHolder.cs

    public class TabItemHolder : DependencyObject, INotifyPropertyChanged
    {
        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(String), typeof(TabItemHolder), new UIPropertyMetadata());
        public String Header
        {
            get { return (String)GetValue(HeaderProperty); }
            set
            {
                SetValue(HeaderProperty, value);
                NotifyPropertyChanged("Header");
            }
        }

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(TabItemHolder), new UIPropertyMetadata());
        public String Text
        {
            get { return (String)GetValue(TextProperty); }
            set
            {
                SetValue(TextProperty, value);
                NotifyPropertyChanged("Text");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(String PropertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }
    }

然后我有了模型类,在本例中是MainWindow.cs本身:

<强> MainWindow.cs

public partial class MainWindow:Window,INotifyPropertyChanged {     public static readonly DependencyProperty SelectedTabProperty = DependencyProperty.Register(“SelectedTab”,typeof(TabItemHolder),typeof(MainWindow),new UIPropertyMetadata());     public TabItemHolder SelectedTab     {         get {return(TabItemHolder)GetValue(SelectedTabProperty); }         组         {             SetValue(SelectedTabProperty,value);             NotifyPropertyChanged( “SelectedTab”);         }     }

public static readonly DependencyProperty TabsProperty = DependencyProperty.Register("Tabs", typeof(ObservableCollection<TabItemHolder>), typeof(MainWindow), new UIPropertyMetadata());
public ObservableCollection<TabItemHolder> Tabs
{
    get { return (ObservableCollection<TabItemHolder>)GetValue(TabsProperty); }
    set
    {
        SetValue(TabsProperty, value);
        NotifyPropertyChanged("Tabs");
    }
}

public MainWindow()
{
    InitializeComponent();
    this.DataContext = this;
    this.Tabs = new ObservableCollection<TabItemHolder>();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    this.Tabs.Add(new TabItemHolder() { Header = "Hello, this is the new tab item!", Text = "Dummy text for the textbox" });
    this.SelectedTab = this.Tabs[this.Tabs.Count - 1];
}

public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String PropertyName)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}

}

最后,XAML会是这样的。

<强> MainWindow.xaml

    <Grid x:Name="LayoutRoot">
        <TabControl x:Name="MyTabControl"
                    Margin="12,67,12,12"
                    ItemsSource="{Binding Tabs}"
                    SelectedItem="{Binding SelectedTab}">
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <Grid>
                        <TextBox Text="{Binding Path=Text}"
                                 Width="200"
                                 HorizontalAlignment="Left"
                                 VerticalAlignment="Top" />
                    </Grid>
                </DataTemplate>
            </TabControl.ContentTemplate>
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Header}"/>
                </DataTemplate>
            </TabControl.ItemTemplate>
        </TabControl>
        <Button Content="Button" Height="34" HorizontalAlignment="Left" Margin="19,12,0,0" Name="button1" VerticalAlignment="Top" Width="90" Click="button1_Click" />
    </Grid>

那会以不同的方式(在我看来更好)的方式做同样的伎俩。

我希望能帮助你。