views:

86

answers:

2

Kind of an odd question- if I'm thinking of this the wrong way please let me know. I am using an infragistics dock manager, which manages tabs as well. So I can create a TabGroupPane, and then add multiple ContentPanes, each of which has its own tab.

In each content pane, I set my viewmodel:

<ContentPane>
  <viewmodels:MyViewModelForTab1 />
</ContentPane>

So here's the problem- while using a mediator pattern for communication, my viewmodels have no idea if they are on the visible tab or not, so they are always working even if hidden. The TabGroupPane does have a 'SelectedTab' property, as well as each ContentPane having an 'IsActive' property.

So the question is how do I set that information in my ViewModel? Making my VM a dependency object seems like a bad idea since I already implement INotifyPropertyChanged. Using a CLR prop in my VM also doesnt work, since you cannot bind to it.

How can I get my VM to know if it is the datacontext of an active tab?

Thanks!

A: 

I would put an IsSelected property on my ViewModel and bind it to the TabItem's IsSelected dependency property.

This should allow you to hook into when it is updated and perform whatever you need. You don't need a mediator pattern here since you are communicatining from the View to the ViewModel.

Make sure your ViewModel is bound to the DataContext property of the view (specifically, that the Tab's DataContext is the ViewModel). The way you have it now, your ViewModel is the content of the element, not bound to the DataContext, as it should be:

<Tab.Resources>
    <viewmodels:MyViewModelForTab1 x:Key="Tab1ViewModel" />
</Tab.Resources>

<ContentPane DataContext="{StaticResource Tab1ViewModel}" />

Or something like that...

codekaizen
I tried that first- but you cannot bind to a non dependency property (as I mentioned). This is the error you get:Error 1 A 'Binding' cannot be set on the 'IsActiveTab' property of type 'MyViewModel'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.Maybe Im doing it wrong?
Nicros
Your binding is probably not right, then. I've implemented this and it works. You need to make sure that the view model is set to be the DataContext of the dependency object. This is a fairly strict requirement of MVVM.
codekaizen
Hmm, Im using my viewmodel with a datatemplate. Here's what I tried in my xaml: <DataTemplate DataType="{x:Type viewmodels:MyViewModel}" > <views:MyView /> </DataTemplate>Then: <viewmodels:MyViewModel IsActiveTab="{Binding ElementName=theContentControl, Path=IsActive}" />
Nicros
You still need to set the ViewModel instance as the DataContext of the View.
codekaizen
Okay let me try and get this to work...
Nicros
Nope Im not getting it. If I set the DataContext of the ContentPane to my viewmodel, where do I access IsActiveTab of my viewmodel? I tried something like:<UserControl.Resources> <vm:MyViewModel x:Key="myVM"/></UserControl.Resources> <igDock:ContentPane DataContext="{StaticResource myVM}"> <ContentControl> <vm:MyViewModel /> </ContentControl></igDock:ContentPane>But I dont know where IsActiveTab should go.... or if I even set the datacontext properly, or if the <vm:MyViewModel /> should be in the ContentControl anymore.
Nicros
A: 

I don't know the Infragistics model, so my apologies if this is inapposite, but here's how I implement this with regular items controls - tab control, list box, whatever.

Create a container view model class that includes an observable collection of items and exposes a SelectedItem property. Make the container class the data context of the items control. Bind the items control's SelectedItem property to the container class's.

Hook the item objects up to the PropertyChanged event of the container. So now when the selected item in the UI changes, the container view model notifies all of the items that SelectedItem has changed. Each item object's event handler can determine for itself whether or not it's now the selected item.

The item objects thus don't know any implementation details of the UI - you can unit test your classes outside of a UI and the logic will still work correctly.

Robert Rossney
Thats an interesting idea, I will explore it as well. I think Im still missing something about how to work with the datacontexts but Im getting there...
Nicros