views:

74

answers:

2

I had to prevent the user from selecting a tabitem in a WPF TabControl,

1)unless and untill the user checks a check box in one condition the user should be shown a message box and if he checks the check box he can navigate to any other tab

2)Checking a particular condition the user shouldnt be able to get into a particular tab on selecting it,and I dont have an option of making the tab item collapse.and it should pop up a message box and get back to the same prv tab item selected

I have seen Smith Josh's sample code as below and this is what i exactly wanted for the 1st scenerio

http://joshsmithonwpf.wordpress.com/2009/09/04/how-to-prevent-a-tabitem-from-being-selected/

But I need something that works in MVVM, where my application has a strict "No CodeBehind"

A: 

You could inherit the TabControl (or add an attached property) which controls if navigation to another tab item is allowed; however, let me just stress that 'no codebehind' is kinda silly - there are plenty of times when code-behind can be used for view-only purposes, and that's ok.

Back to the problem... what you'd do using my suggestion is hide the code-behind (checking if the action is allowed) inside a control, so that the actual view (the page/window etc) doesn't contain it. If you declare the new property as a DependencyProperty you get all the binding facilities etc.

Alex Paven
@Alex, ya I am ready to use even the code behind , but all the data dn data context are being driven from the view model, this is in a way obstructing me to just seperate all the implementation from the viewmodel to codebehind.
crazy9
A: 

EDIT: I tested my other code and it didn't work. Was just an idea anyways. Here's a method that does work (although I agree with Alex that code behind in MVVM is fine when adjusting the View).

In this case I created a converter which takes two boolean values: If the tab is selected and if we can change tabs. If both of these are set to false, we return false to disable the tab. If either is set to true, we leave the tab enabled.

Here's the code. I have a property in my VM called CanChangeTabs and an instance of MyConverter in Window.Resources called Converter.

XAML inTabItem:

<TabItem.IsEnabled>
    <MultiBinding Converter="{StaticResource Converter}">
        <Binding RelativeSource="{RelativeSource Self}" Path="IsSelected" />
        <Binding Path="CanChangeTabs" />
    </MultiBinding>
</TabItem.IsEnabled>

Converter:

public class MyConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        foreach (object value in values)
        {
            if ((bool)value)
            {
                return true;
            }
        }
        return false;
    }

    public object[] ConvertBack(object values, Type[] targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Rachel
ya tried the code but, Throwing an error "The attached property Style.Triggers cannot be defined in setter or one of its base classes"
crazy9
Yeah I didn't throw the code through an editor and forgot an end tag for the setter. I decided to try the code out and it didn't work anyways so I updated the post with a working solution.
Rachel
@Rachel, The code do work but the only problem is , only after I get into the particilar tab, it is getting disabled,I gues this is bcoz the coverter is getting implemeted on Is enabled only after the selcetion on that particular tab item.
crazy9