将Tabitem标题设置为Wpf中的Menuitem标题

时间:2015-07-16 12:59:48

标签: wpf mvvm viewmodel workspace

我正在使用WPF开发桌面软件,我想使用制表符控件来查看不同的表单。我也在研究MVVM。每次单击菜单时,我都设法创建一个新的选项卡控件。如何将tabitem标题设置为与menuitem单击标题相同。这是我到目前为止所做的代码!

以下是观点:

 <MenuItem Header="_File">
     <MenuItem Command="{Binding NewWorkspaceCommand}" Header="_New Tab" InputGestureText="Ctrl + N" />
     <MenuItem Command="{Binding CloseWorkspaceCommand}" Header="_Close Tab" InputGestureText="Ctrl + F4" />
     <MenuItem Command="{Binding ExitCommand}" Header="E_xit" InputGestureText="Ctrl + X" />
 </MenuItem>



<TabControl x:Name="tbMain" ItemsSource="{Binding Workspaces}" SelectedIndex="{Binding SelectedIndex}" Opacity=" 0.90"  VerticalAlignment="Top" Height="{Binding Path=ActualHeight,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Window }}}">
    <TabControl.Background >
    <ImageBrush ImageSource="/Resources/images/background.png" Stretch="UniformToFill" />
</TabControl.Background>

<TabControl.ItemTemplate>
    <DataTemplate>
        <WrapPanel>
            <TextBlock Text="{Binding Header}" />
            <Button Command="{Binding CloseCommand}" Content="X" Margin="4,0,0,0" FontFamily="Courier New" Width="17" Height="17" VerticalContentAlignment="Center" />
        </WrapPanel>
    </DataTemplate>
</TabControl >

查看模型

public class MainViewModel : ViewModelBase
{ 

    #region Constructor

    public MainViewModel()
    {
        invokemenuClick;
        Workspaces = new ObservableCollection<WorkspaceViewModel>();
        Workspaces.CollectionChanged += Workspaces_CollectionChanged;

    }

    #endregion

    #region Event Handlers

    void Workspaces_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        if (e.NewItems != null && e.NewItems.Count != 0)
            foreach (WorkspaceViewModel workspace in e.NewItems)
                workspace.RequestClose += this.OnWorkspaceRequestClose;

        if (e.OldItems != null && e.OldItems.Count != 0)
            foreach (WorkspaceViewModel workspace in e.OldItems)
                workspace.RequestClose -= this.OnWorkspaceRequestClose;
    }

    private void OnWorkspaceRequestClose(object sender, EventArgs e)
    {
        CloseWorkspace();
    }

    #endregion

    #region Commands

    private DelegateCommand _exitCommand;
    public ICommand ExitCommand
    {
        get { return _exitCommand ?? (_exitCommand = new DelegateCommand(() => Application.Current.Shutdown())); }
    }

    private DelegateCommand _newWorkspaceCommand;
    public ICommand NewWorkspaceCommand
    {
        get { return _newWorkspaceCommand ?? (_newWorkspaceCommand = new DelegateCommand(NewWorkspace)); }
    }

    private void NewWorkspace()
    {
        frmCustomer oCustomer = new frmCustomer();

        var workspace = new WorkspaceViewModel { Header = "New Tab" } ;

        Workspaces.Add(workspace); 
        /// = oCustomer.Content; 
        SelectedIndex = Workspaces.IndexOf(workspace);

    }

    private DelegateCommand _closeWorkspaceCommand;
    public ICommand CloseWorkspaceCommand
    {
        get { return _closeWorkspaceCommand ?? (_closeWorkspaceCommand = new DelegateCommand(CloseWorkspace, () => Workspaces.Count > 0)); }
    }

    private void CloseWorkspace()
    {
        Workspaces.RemoveAt(SelectedIndex);
        SelectedIndex = 0;
    }


    #endregion

我想设置调用菜单标题,其中显示var workspace = new WorkspaceViewModel { Header = "New Tab" } ;其中&#34;新标签&#34;是调用menuItem的名称。

我希望我已经解释过了。

Thanx提前为您提供帮助! `

0 个答案:

没有答案