本文介绍了协助我在XAML来定义用户界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码动态添加TabItem的TabControl的到:

this is my code to dynamically add tabitem to tabcontrol:

TabItem newTab = new TabItem();
newTab.Header = ncn.courseName;
newTab.FontSize = 20;
TextBox textbox = new TextBox();
textbox.HorizontalAlignment = HorizontalAlignment.Stretch;
textbox.VerticalAlignment = VerticalAlignment.Stretch;
textbox.FontSize = 12;
textbox.AcceptsReturn = true;
newTab.Content = textbox;
this.Courses.Items.Add(newTab);
this.Courses.SelectedItem = newTab;



我觉得可能是更好的方法来做到这一点(即定义在XAML的UI)。我是新来WPF和无法弄清楚模板的事情是如何工作的。 !所以,请帮我

I think there might be better way to do this (ie, define the UI in xaml). I'm new to WPF and couldn't figure out how the template thing works. So please help me!

注:TabControl的是在开始空(深藏不露,不TabItem的,没有文本框)!我要添加新的标签只有当我点击添加按钮。

NOTE: the tabcontrol is empty at the beginning (showing nothing, no tabitem, no textbox)! I want to add new tab only when I click the "add" button.

有人帮我,想通了。

推荐答案

的第一件事是:它是在所有情况下,99%的最好的,可能你写UI的XAML

The first thing is: It is in 99% of all cases the best and possible to write you UI in XAML.

其次,你应该告诉自己,什么WPF的指示和如何使用的。 !如果你没有,你应该留在Windows窗体

Second you should inform yourself, whats the WPF indicates and how to use the MVVM. If you don't you should stay by Windows Forms!

所以现在:

您视图模型:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PB.Base;

namespace DynTabItems
{
    public class MainWindowViewModel
    {
        public MainWindowViewModel()
        {
            AddItemCommand = new DelegateCommand(AddItem, CanAddItem);
            RemoveItemCommand = new DelegateCommand(RemoveItem, CanRemoveItem);
        }

        public DelegateCommand AddItemCommand { private set; get; }
        public DelegateCommand RemoveItemCommand { private set; get; }

        ObservableCollection<MyModel> _yourBehaviorClass = new ObservableCollection<MyModel>();
        MyModel _selectetItem;

        public MyModel SelectetItem
        {
            get { return _selectetItem; }
            set { _selectetItem = value; }
        }

        public ObservableCollection<MyModel> YourBehaviorClass
        {
            get { return _yourBehaviorClass; }
            set { _yourBehaviorClass = value; }
        }

        private void AddItem(object obj)
        {
            YourBehaviorClass.Add(new MyModel());
        }

        private bool CanRemoveItem(object obj)
        {
            return SelectetItem != null;
        }

        private bool CanAddItem(object obj)
        {
            return true;
        }

        private void RemoveItem(object obj)
        {
            YourBehaviorClass.Remove(SelectetItem);
        }
    }
}



现在你的XAML代码:

And now your XAML Code:

<Window x:Class="DynTabItems.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Button Content="Add" Command="{Binding AddItemCommand}"/>
        <Button Content="Remove" Command="{Binding RemoveItemCommand}"/>
        <TabControl ItemsSource="{Binding YourBehaviorClass}" SelectedItem="{Binding SelectetItem}">
            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="Header" Value="{Binding YourHeader, UpdateSourceTrigger=PropertyChanged}"/>
                    <Setter Property="Content">
                        <Setter.Value>
                            <StackPanel>
                                <TextBox Text="{Binding YourText, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">
                                    <ToolTipService.ToolTip>
                                        <TextBlock Text="{Binding Error}"/>
                                    </ToolTipService.ToolTip>
                                </TextBox>
                            </StackPanel>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TabControl.ItemContainerStyle> 
        </TabControl>
    </StackPanel>
</Window>

和最后但并非最不重要为MyModel:

And last but not least MyModel:

using PB.Base;
using PB.Interfaces;
using PB.ValidationError;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DynTabItems
{
    public class MyModel : ViewModelBaseEx<MyModel>
    {
        public MyModel()
        {
            YourHeader = "You header String";
            ListOfExistingErrors = new ObservableCollection<IValidationErrorInfo<MyModel>>();
            ListOfExistingErrors.Add(new Error<MyModel>() { ErrorIndicator = "YourText", ErrorText = "Your Error Text", Predicate = s => string.IsNullOrEmpty(s.YourText) });
        }

        string _yourText;
        string _yourHeader;

        public string YourHeader
        {
            get { return _yourHeader; }
            set
            { 
                _yourHeader = value;
                SendPropertyChanged(() => YourHeader);
            }
        }

        public string YourText
        {
            get { return _yourText; }
            set 
            { 
                _yourText = value;
                SendPropertyChanged(() => YourText);
            }
        }

        public override ObservableCollection<IValidationErrorInfo<MyModel>> ListOfExistingErrors
        {
            get;
            set;
        }
    }
}

如果您需要的例子项目与我联系。

If you need the example Project contact me.

这篇关于协助我在XAML来定义用户界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 14:59