-5

これは、tabitemを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を初めて使用し、テンプレートがどのように機能するかを理解できませんでした。だから私を助けてください!

注:タブコントロールは最初は空です(何も表示されず、タブアイテムもテキストボックスも表示されません)。「追加」ボタンをクリックしたときだけ新しいタブを追加したい。


誰かが私を助けて、それを理解しました。

4

1 に答える 1

0

まず、99% の場合、XAML で UI を作成するのが最善であり、可能です。

次に、WPF が示す内容とMVVMの使用方法を自分自身に通知する必要があります。そうでない場合は、Windows フォームを使用する必要があります。

だから今:

あなたViewModel:

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 コード:

<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:

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;
        }
    }
}

サンプル プロジェクトが必要な場合は、私に連絡してください。

于 2013-01-16T20:03:41.427 に答える