繁体   English   中英

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

[英]MVVM WPF Adding TabItem From Another TabItem's Content

您好,很长的帖子。很抱歉,希望没人问同样的问题。 如果是这样,请问您的借口。 我在使用MVVM方法从另一个TabItem的内容动态添加TabItem遇到了问题,例如UserControl MainWindow绑定了一个名为TabsMainViewModel的viewmodel类:

<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属性中,我设置了一个出现在TabItem内容中的UserControl 我的问题是:我怎样才能把这个按钮CustomerSearch用户控件,而按钮将绑定类似ICommandDelegateCommand (在其CustomerSearchViewModel为例),可以执行AddItem函数TabsMainViewModel

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

我已经解决了我的问题。 我将发布解决方案,以供再次检查,以及是否有人遇到相同的问题。 因此解决方案是:

添加一个名为CustomerSearchViewModel的类,它是CustomerSearch的视图模型。 在此类中,您添加一个委托函数和一个在TabsMainViewModel注册的事件:

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

        }

    }
}

在我创建UserControl实例的主视图TabsMainViewModel中,我注册了该事件:

public ICommand CustomerSearch
        {
            get
            {
                CustomerSearch f = new CustomerSearch() { UniqueTabName = "NewTab1", Title = "Customer Search" };
                CustomerSearchViewModel.AddNewCustomerTab += AddItem;
                return new DelegateCommand(delegate { this.AddItem(f); });
            }

        }

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

最好的祝福,

朱利安

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM