4

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"

1
  • +1 for the link to the Josh Smiths article. Great!
    – HCL
    Commented Aug 28, 2010 at 8:53

2 Answers 2

0

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.

1
  • 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
    Commented Aug 28, 2010 at 22:41
0

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();
    }
}
3
  • 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
    Commented Aug 27, 2010 at 17:20
  • 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
    Commented Aug 27, 2010 at 18:43
  • 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
    Commented Aug 28, 2010 at 21:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.