WPF 选项卡控件:混合项源和项



我正在将GroupViewModel(GroupModel是一个商业概念)绑定到TabControl.ItemsSource

我需要在此 TabControl 的末尾添加一个具有 (+) 符号的选项卡,类似于 Web 浏览器上用于添加新选项卡的最后一个选项卡。

我想我可以将TabControl.ItemsSource绑定到GroupViewModel集合以构建我的选项卡,然后调用 TabControl.Items.Add 并为最后一个选项卡添加一个TabItem带有 (+) 的选项卡以添加更多选项卡,如下所示:

TabItem tabItem = new TabItem();
tabItem.Header = "+";
TabControlDynamic.Items.Add(tabItem);

但是,这给了我一个错误:

Operation is not valid while ItemsSource is in use. 
Access and modify elements with ItemsControl.ItemsSource instead.

我想我可以在我的集合末尾添加一个"虚拟"GroupViewModel,并取消所有与业务相关的值,以便获得最后一个选项卡,但这感觉不对。 我一直在想一定有更好的方法。

这是我唯一的选择吗? 有什么方法可以在最后创建一个选项卡,而不必陪审团操纵我的 ViewModel 和相应的模型?

谢谢

菲利普

我已经尝试过这样:

XAML:

 <TabControl ItemsSource="{Binding Items}" SelectionChanged="TabControl_SelectionChanged_1">
                <TabControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Header}"/>
                    </DataTemplate>
                </TabControl.ItemTemplate>
                <TabControl.ContentTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Content}"/>
                    </DataTemplate>
                </TabControl.ContentTemplate>
            </TabControl>

代码隐藏:

 private void TabControl_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            TabControl control = sender as TabControl;
            if (control != null && control.SelectedItem is Tab)
            {
                if ((control.SelectedItem as Tab).Header == " ")
                {
                    (control.SelectedItem as Tab).Header = "New Tab";
                    (control.DataContext as TabViewModel).Items.Add(new Tab() { Header = " ", Content = "" });
                    control.UpdateLayout();
                }
            }
        }

视图模型:

internal class TabViewModel : INotifyPropertyChanged
    {
        public void RaisePropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<Tab> items;
        public ObservableCollection<Tab> Items
        {
            get { return items; }
            set { items = value; RaisePropertyChanged("Items"); }
        }
        public TabViewModel()
        {
            items = new ObservableCollection<Tab>();
            items.Add(new Tab() { Header = "Tab Item 1", Content = "This is content 1" });
            items.Add(new Tab() { Header = "Tab Item 2", Content = "This is content 2" });
            items.Add(new Tab() { Header = "Tab Item 3", Content = "This is content 3" });
            items.Add(new Tab() { Header = " ", Content = "" });
        }
    }
    public class Tab:INotifyPropertyChanged
    {
        private string header;
        public string Header
        {
            get { return header; }
            set { header = value; RaisePropertyChanged("Header"); }
        }
        private string content;
        public string Content
        {
            get { return content; }
            set { content = value; RaisePropertyChanged("Content"); }
        }
        public void RaisePropertyChanged(string propertyname)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

最新更新