Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8099653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:28:49+00:00 2026-06-05T22:28:49+00:00

I’ve recently been learning the MVVM pattern in WPF and just started making my

  • 0

I’ve recently been learning the MVVM pattern in WPF and just started making my first proper, rather big application. So far it’s all smooth sailing, and I’m liking what I’m seeing a lot. However I recently met something of a stumbling block.

The application is built with a main TabControl, each TabItem containing a pretty big details view.

TabControl inside main View, ItemsSource bound to MainViewModel.OpenTabs
 TabItem with data specific View+ViewModel
 TabItem with data specific View+ViewModel
 TabItem with data specific View+ViewModel
 etc...

The OpenTabs collection is an ObservableCollection<BaseViewModel> on MainViewModel, and the TabControl‘s SelectedItem is bound to MainViewModel.ActiveTab.

So far so good! However, what I’m not sure I’m getting is how to handle closing of tabs while at the same time following MVVM. If I wasn’t trying to be strict with the MVVM (in order to learn it properly), I’d just bind a MouseDown-event on the TabItem-headers and thus get a reference to the clicked item in that event, removing it from the OpenTabs collection in that way. But – unless I’m mistaken – the interaction logic shouldn’t need references to actual UI items in order to be effective and proper MVVM.

So, how do I handle this MVVM style? Do I use a command that sends a specific parameter with it to my MainViewModel? It seems like the preferred implementation of ICommand in MVVM doesn’t take object parameters (looking at MVVM Light as well as some other tutorials).

Should I just create a CloseTab(int id) public method on my MainViewModel and call that from the view codebehind after catching the Click on my TabItem close button? This seems like MVVM-cheating. 🙂

Also a final note – this should work even if I click close on a TabItem that isn’t the currently active one. Otherwise it wouldn’t be hard to setup with OpenTabs.Remove(ActiveTab).

Thanks for any help! I’d also appreciate any links to recommended reading/watching regarding these problems.

Solution: It seems the best way is to use a command that can accept command parameters. I used the RelayCommand from MVVM Light framework:

In MainViewModel:

CloseTabCommand = new RelayCommand<BaseViewModel>((vm) =>
{
    OpenTabs.Remove(vm);
});

In XAML:

<Button
Command="{Binding Source={StaticResource MainViewModel}, Path=CloseTabCommand}"
CommandParameter="{Binding}">

Note: Your binding paths may of course vary depending on how your Views and ViewModels are set up.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:28:50+00:00Added an answer on June 5, 2026 at 10:28 pm

    The best and the right way is to create the command. In different frameworks ICommand usually has two implementation, with the parameter and without one (as often you do not need it).

    MVVM light has two ICommand implementation as well: RelayCommand and RelayCommand<T>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.