WPF Tabcontrol:混合ItemsSource和Items

时间:2014-03-03 00:53:59

标签: c# wpf tabs

我将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和相应的模型?

谢谢,

菲利普

1 个答案:

答案 0 :(得分:1)

我试过这样的话:

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