Closing nested tabs using WPF?

2019-06-08 04:06发布

问题:

I'm trying to design a UI that contains nested tabs, but I can't seem to find information on how to remove the nested tabs in an MVVM fashion.

Here's my XAML (snipped for brevity):

<TabControl Name="ProjectTabControl" DockPanel.Dock="Top" ItemsSource="{Binding ProjectTabs}" IsSynchronizedWithCurrentItem="True">
   <TabControl.ContentTemplate>
      <DataTemplate>
         <TabControl DockPanel.Dock="Top" ItemsSource="{Binding FileTabs}" Padding="1">
            <Button Command="{Binding CloseTabCommand}" CommandParameter="TabItem" />
         </TabControl>
      </DataTemplate>
   </TabControl.ContentTemplate>
</TabControl>

My question is: what argument(s?) should I be passing in CommandParameter, and how would I affect it in the ViewModel to properly remove the tab?

回答1:

It doesn't look like you have a solid understanding of how to implement an M-V-VM pattern in WPF. It looks like you know understand DataBinding but the pattern your sample demonstrates seems to be missing an understand of ViewModels and Commands, which are really required for MVVM.

Review the article linked below and the sample application it uses. You should find what you're looking to do is demonstrated here and is pretty easy to implement.

The Model-View-ViewModel (MVVM) Design Pattern for WPF

I hope that this doesn't seem like a cop-out but it would require a significant amount of re-posting of the linked article to get you where you want to go. Go read the article, work through the sample and if you still have problems, post on this question and I'll make sure to watch for it.