MVVM WPF从另一个TabItem的内容添加TabItem

时间:2015-01-16 12:08:58

标签: c# wpf xaml mvvm

你好,很抱歉这篇文章很长。我希望没有人问过同样的问题。如果是这样,我会找你的借口。 我使用MVVM方法从另一个TabItem的内容动态添加TabItem时遇到问题,这是UserControlMainWindow有一个名为TabsMainViewModel的视图模型类绑定:

<Window x:Name="Main" x:Class="Interface_test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="clr-namespace:Interface_test"
        xmlns:uc="clr-namespace:Interface_test.Customers"
        Title="MainWindow" Height="850" Width="825" WindowStartupLocation="CenterScreen" WindowState="Maximized">
    <Window.DataContext>
        <vm:TabsMainViewModel />
    </Window.DataContext>
    <Window.Resources>
    <DataTemplate x:Key="TabItemTemplate">
        <DockPanel>
            <Button Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" 
                    Background="Transparent" 
                    Name="btnDelete" 
                    DockPanel.Dock="Right"
                    Margin="5,0,0,0" 
                    Padding="0"  
                    Command="{Binding RemoveItemCommand}">
                <Image Height="11" Width="11" Source="Images/closeButton.png"/>
            </Button>
            <TextBlock Text="{Binding Header}" />
        </DockPanel>
    </DataTemplate>
    <DataTemplate x:Key="TabItemContent" >
        <UserControl Content="{Binding TabContent}"/>
    </DataTemplate>
    <Style TargetType="TabItem">
        <Setter Property="IsSelected"
                Value="{Binding IsSelected, Mode=TwoWay}"/>
    </Style>
</Window.Resources>
<TabControl  ItemTemplate="{StaticResource TabItemTemplate}"
                 ContentTemplate="{StaticResource TabItemContent}"
                 ItemsSource="{Binding Tabs}"
                 HorizontalAlignment="Stretch" 
                 VerticalAlignment="Stretch" 
                 VerticalContentAlignment="Stretch" 
                 HorizontalContentAlignment="Stretch"  
                 Name="tcMDI" 
                 Visibility="Visible"  
                 ScrollViewer.HorizontalScrollBarVisibility="Auto" 
                 ScrollViewer.VerticalScrollBarVisibility="Auto"  >

TabsMainViewModel我有一个名为ObservableCollection的自定义类TabViewModel

public class TabsMainViewModel
    {
        int tabCounter;
        private Dictionary<string, string> _openedTabs = new Dictionary<string, string>();
        public TabsMainViewModel()
        {
            this.Tabs=new ObservableCollection<TabViewModel>();
            //this.AddItem(null);
        }
        public ObservableCollection<TabViewModel> Tabs
        {
            get;
            private set;
        }
        public ICommand CustomerSearch
        {
            get
            {
                CustomerSearch f = new CustomerSearch() { UniqueTabName = "NewTab1", Title = "Customer Search" };
                return new DelegateCommand(delegate { this.AddItem(f); });
            }

        }
        public ICommand Customer
        {
            get
            {
                Customer f = new Customer() { UniqueTabName = "NewTab2", Title = "Customer" };
                return new DelegateCommand(delegate { this.AddItem(f); });
            }

        }
        public ICommand EmployerSearch
        {
            get
            {
                CustomerSearch f = new CustomerSearch() { UniqueTabName = "NewTab3", Title = "Employer Search" };
                return new DelegateCommand(delegate { this.AddItem(f); });
            }
        }
        public ICommand Employer
        {
            get
            {
                Customer f = new Customer() { UniqueTabName = "NewTab4", Title = "Employer" };
                return new DelegateCommand(delegate { this.AddItem(f); });
            }

        }
        public void AddItem(ITabContent userControl)
        {

            if (_openedTabs.ContainsKey(userControl.UniqueTabName))
            {
                foreach (TabViewModel tvm in Tabs)
                {
                    if (userControl.UniqueTabName == tvm.TabContent.UniqueTabName)
                    {
                        tvm.IsSelected = true;
                        break;
                    }
                }
            }
            else
            {
                TabViewModel tabItem = new TabViewModel(this) { TabContent = userControl };
                tabItem.TabContent.Title += " " + tabCounter;
                tabItem.IsSelected = true;
                Tabs.Add(tabItem);
                _openedTabs.Add(tabItem.TabContent.UniqueTabName, tabItem.TabContent.Title);
                tabCounter++;
            }

        }
        public void RemoveItem(TabViewModel tabItem)
        {
            this.Tabs.Remove(tabItem);
            _openedTabs.Remove(tabItem.TabContent.UniqueTabName);
            tabItem.Dispose();

        }
    }
}

TabViewModel班级,我有:

public class TabViewModel:ObservableObject,IDisposable
    {
        private bool _isSelected;
        private ITabContent _tabContent;
        private readonly TabsMainViewModel tabsMainViewModel;

        public TabViewModel(TabsMainViewModel tabsMainViewModel)
        {
            this.tabsMainViewModel = tabsMainViewModel;
            this.tabsMainViewModel.Tabs.CollectionChanged += this.Tabs_CollectionChanged;


            this.RemoveItemCommand = new DelegateCommand(
                delegate
                {
                    this.tabsMainViewModel.RemoveItem(this);
                },
                delegate
                {
                    return this.tabsMainViewModel.Tabs.Count > 1;
                }
                );
        }
        public void Dispose()
        {
            this.tabsMainViewModel.Tabs.CollectionChanged -= this.Tabs_CollectionChanged;
        }
        private void Tabs_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            this.RemoveItemCommand.RaiseCanExecuteChanged();
        }

        public DelegateCommand RemoveItemCommand { get; set; }

        public ITabContent TabContent
        {
            get { return _tabContent;}
            set
            {
                _tabContent = value;
                _tabContent.Parent = this;
                Header = value.Title;
            }
        }
        public String Header
        {
            get;
            private set;
        }
        public bool IsSelected
        {
            get { return this._isSelected; }
            set
            {
                if (this._isSelected != value)
                {
                    this._isSelected = value;
                    RaisePropertyChangedEvent("IsSelected");
                }
            }
        }
    }
}

TabContent媒体资源中,我设置了UserControl内容中显示的TabItem。我的问题是:如何在此CustomerSearch用户控件中添加一个按钮,并将该按钮绑定为ICommandDelegateCommand(例如,在其CustomerSearchViewModel中)可以绑定的按钮执行AddItem中的TabsMainViewModel功能?

如果需要,我可以发布ObservableObjectDelegateCommand类 和ITabContent接口。 提前致谢

1 个答案:

答案 0 :(得分:0)

我很乐意解决我的问题。我会发布解决方案,进行双重检查,如果有人遇到同样的问题。所以解决方案是:

添加一个名为CustomerSearch的{​​{1}}视图模型的类。在此类中,您将添加委托函数以及在CustomerSearchViewModel

中注册的事件
TabsMainViewModel

在我创建UserControl实例的主视图模型namespace Interface_test.Customers { public delegate void OpenNewTab(ITabContent uc); class CustomerSearchViewModel { public static event OpenNewTab AddNewCustomerTab = delegate { }; public CustomerSearchViewModel() { } public ICommand ShowCustomer { get { Customer f = new Customer() { UniqueTabName = "NewTab12", Title = "Customer from search" }; return new DelegateCommand(delegate {AddNewCustomerTab(f); }); } } } } 中,我注册了该事件:

TabsMainViewModel

我不知道这是否是正确的方法,但它对我有用。如果有人有最好的解决方案,请不要犹豫,分享它。

最诚挚的问候,

儒略