Prevent user selection of WPF Tab

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

1) if and until the user checks the checkbox in one condition, the user should be shown a message box, and if he checks the checkbox, he can go to any other tab

2) By checking a specific condition, the user should not be able to get to a certain tab when it is selected, and I have no way to make a tab element collapse.and he should display a message box and return to the same position of the prv tab

I saw Smith Josh sample code as shown below and this is what I definitely wanted for the first 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"

+4
source share
2 answers

You can inherit a TabControl (or add a nested property) that controls if movement to another tab element is allowed; however, let me just emphasize that "no codebehind" is not stupid - there are many times when the code code can be used for viewing purposes only, and this is normal.

Let's get back to the problem ... what you would do using my suggestion is to hide the code (check if the action is allowed) inside the control, so the actual view (page / window, etc.) doesn’t contain if you declare the new property as DependencyProperty, you get all the necessary bindings, etc.

0
source

EDIT: I tested my other code and it did not work. It was just an idea. Here's a method that works (although I agree with Alex that the code in MVVM is good at setting up the View).

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

Here is the code. I have a property in my VM called CanChangeTabs and an instance of MyConverter in Window.Resources is 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(); } } 
0
source

All Articles