0

I've been adapting Josh Smith's MVVM Demo app to suit my requirements.

I have been successful for most part of it. Now I want to add a feature "Print"(similar to a print feature that exsits in any application) in the file menu item for this applicaiton. Since I can open multiple tabs in this application, how will I know which tab(to be more specific which Workspace) was active when the user clicked the "Print"? The code below shows the DataTemplate where the TabControl is used in the DemoApp.

Any help/thoughts are greatly appreciated.

<DataTemplate x:Key="WorkspacesTemplate">
<TabControl 
  IsSynchronizedWithCurrentItem="True" 
  ItemsSource="{Binding}" 
  ItemTemplate="{StaticResource ClosableTabItemTemplate}"
  Margin="4"
  />

savi
  • 497
  • 2
  • 12
  • 27
  • http://stackoverflow.com/questions/4255017/wpf-tab-control-how-do-i-get-the-currently-selected-tab – dev hedgehog Jan 20 '15 at 08:05
  • @devhedgehog : No this is not how things work in MVVM. MVVM does not have code behind, the stakoverflow link that you suggest works for general WPF solutions. – savi Jan 20 '15 at 17:29
  • Where did you read that? model-VIEW-ViewModel pattern sure allows code behind my friend code behind is part of the VIEW. Futhermore if you check out that link properly the user binds TabControl.SelectedItem to a property in ViewModel. There is no code behind at all there :) – dev hedgehog Jan 21 '15 at 08:08
  • But the stackoverflow link does not work for MVVM architecture! I did try out the first answer in this link http://stackoverflow.com/questions/19857184/wpf-tab-control-and-mvvm-selection, but it does not work for me. – savi Jan 21 '15 at 22:22
  • 1
    SelectedIndex is not working for you and also SelectedItem? Well then your ViewModel needs improvements. Post more code please – dev hedgehog Jan 22 '15 at 07:38
  • @devhedgehog Can we continue this conversation in a chat please? – savi Jan 28 '15 at 20:07
  • @devhedgehog I work in the Pacific time zone, I am not sure about you. Also I do not have sufficient reputation to invite you to chat in SO. – savi Jan 29 '15 at 18:04
  • 1
    Run your code and check output window. I think there you will stumble upon few binding errors – dev hedgehog Jan 30 '15 at 08:05
  • @devhedgehog In JoshSmith's MvvmDemoApp example let us say there is a print option in the File menuItem in the MainWindow, which prints the current active tab. How do I do that? – savi Jan 30 '15 at 20:09
  • Just check that guys code if you already have his example :) :) :) – dev hedgehog Feb 03 '15 at 07:34
  • @devhedgehog he does not have this feature implemented! – savi Feb 03 '15 at 17:25
  • I found this example using google :) try it out https://code.msdn.microsoft.com/windowsdesktop/WPF-Printing-Overview-f28c541a – dev hedgehog Feb 05 '15 at 07:03

1 Answers1

0

First of all your DataContext is wrong, your DataContext should be a ViewModel, to which you bind the ItemsSource to a ObservableCollection Property. E.g.

<TabControl 
  IsSynchronizedWithCurrentItem="True" 
  ItemsSource="{Binding Tabs}" 
  SelectedItem={Binding SelectedTab}
  ItemTemplate="{StaticResource ClosableTabItemTemplate}"
  Margin="4"
  />

Where you have

public class MainViewModel 
{
    public ObservableCollection<TabViewModel> Tabs { get; set; }
    public TabViewModel SelectedTab { get; set; }
}

And in your window code behind, all you should have in the constructor is

public MainView()
{
    this.DataContext = new MainViewModel()
}

or

public MainView(MainViewModel vm)
{
    this.DataContext = vm;
}
Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118
  • This is code is from Josh Smith's example, https://msdn.microsoft.com/en-us/magazine/dd419663.aspx . It works fine, just that I am unable to get the active tab. When I try your approach, nothing is shown in the tab. – savi Jan 28 '15 at 20:39