Hi,
I am using xamTabControl. I wanted to stop user navigating to other tabitem in tabcontrol.
Ex. If I am in "Tab1" and user is doing some operation. If user will click on the "Tab2", I will show a confirmation message. If user says "yes" I will navigate to "Tab2", else I will retain in "Tab1".
Could any one please let me know, how to achive this?
Thank You,
Regards,
Ritesh
Hello Ritesh,
You could probably handle the ExecutedCommand event and see if the command that is currently executing is TabControlCommands.SelectNextTab or TabControlCommands.SelectPreviousTab and cancel it.
Thank you for the answer. But the answer did not work for me.
I handled the Execute Command event as below
private
e)
but the problem is event is not executing.
I have give an break point there, in the event, break point is not firing.
Thankx.
Interesting, yes. In this case, you can handle the SelectionChangin event to display the confirmation dialog box and store the last selected item in a variable. If the result is false, you can go back to the last selected item
This works but, the selectedindex event fires twice. Which is again a problem.
Regards
Diid any one have luck on this?
I am sure there is a better way to do it, but I seemed to have managed a pattern that works. The trick for me was using PreviewKeyDown and PreviewMouseDown in conjunction with SelectionChanged.
public AspOptionList()
{
InitializeComponent();
Loaded +=
new RoutedEventHandler(AspOptionList_Loaded);
AspOptionTabList.PreviewMouseDown +=
new MouseButtonEventHandler(AspOptionTabList_PreviewMouseDown);
AspOptionTabList.PreviewKeyDown +=
new KeyEventHandler(AspOptionTabList_PreviewKeyDown);
AspOptionTabList.SelectionChanged +=
new SelectionChangedEventHandler(AspOptionTabList_SelectionChanged);
}
bool
_LoadControl = true;
int _LastTabIndex = 0, _CurrTabIndex = 0;
void AspOptionTabList_PreviewKeyDown(object sender, KeyEventArgs e)
if (
Keyboard.IsKeyDown(Key.LeftShift) && Keyboard.IsKeyDown(Key.Tab) ||
Keyboard.IsKeyDown(Key.RightShift) && Keyboard.IsKeyDown(Key.Tab) ||
e.Key ==
Key.Right || e.Key == Key.Left
)
if (!VerifyTabComplete())
e.Handled =
true;
return;
switch (_LastTabName)
case "AspProcessView":
SetViewControl(AspProcessViewControl,
"AspProcessView");
break;
default:
SetViewControl(AspPipelineProcessViewControl,
"AspPipelineProcessView");
_LastTabIndex = _CurrTabIndex;
_LastTabName = _CurrTabName;
void AspOptionTabList_PreviewMouseDown(object sender, MouseButtonEventArgs e)
bool VerifyTabComplete()
string messageBoxText = "Changes will be lost. Continue?";
string caption = "";
System.Windows.
MessageBoxButton button = System.Windows.MessageBoxButton.OKCancel;
MessageBoxImage image = System.Windows.MessageBoxImage.Question;
MessageBoxResult defaultResult = System.Windows.MessageBoxResult.Cancel;
MessageBoxOptions options = System.Windows.MessageBoxOptions.DefaultDesktopOnly;
bool isDirtyData = false;
if (AspProcessViewControl.ViewModel.ViewIsDirty)
isDirtyData =
if (AspPipelineProcessViewControl.ViewModel.ViewIsDirty)
if (isDirtyData)
if (System.Windows.MessageBox.Show(messageBoxText, caption, button, image, defaultResult, options) == System.Windows.MessageBoxResult.Cancel)
_LoadControl =
false;
AspOptionTabList.SelectedItem = AspOptionTabList.Items[_LastTabIndex];
return false;
return true;
private void AspOptionTabList_SelectionChanged(object sender, RoutedEventArgs e)
_CurrTabName = ((System.Windows.Controls.
HeaderedContentControl)(((System.Windows.Controls.Primitives.Selector)(sender)).SelectedItem)).Header.ToString();
if (AspOptionTabList.SelectedItem != null)
IBaseView view;
if (AspPipelineProcessViewControl != null)
AspPipelineProcessViewControl.Visibility = System.Windows.
Visibility.Hidden;
if (AspProcessViewControl != null)
AspProcessViewControl.Visibility = System.Windows.
switch (_CurrTabName)
_CurrTabIndex = ((System.Windows.Controls.Primitives.
Selector)(sender)).SelectedIndex;
view = SetViewControl(AspProcessViewControl,
((
AspProcessView)view).InitializeControl();
AspProcessViewControl = (
AspProcessView)view;
PipelineProcessContent.Content = view;
view = SetViewControl(AspPipelineProcessViewControl,
AspPipelineProcessView)view).InitializeControl();
AspPipelineProcessViewControl = (
AspPipelineProcessView)view;
private IBaseView SetViewControl(IBaseView view, string name)
if (view == null)
view =
ClientFactory.GetView(name) as BaseView;
BaseView)view).AncestorParent = this;
view.ParentView = ParentView;
if (_LoadControl)
view.ViewModel = ((
AspOptionViewModel)ViewModel).SetAspViewModel(name);
return view;
string _LastTabName = string.Empty, _CurrTabName = string.Empty;
This is critical. Does anyone know the answer? I need to prevent user from going to another tab when they have unsaved data and to prompt them and ask them first. The problem I am having is that no matter what, the tab they clicked on wants to be the active tab, event when I try e.handled = true.
I put a message box question in there, and then try to redirect the tab by setting the SelectedItem property, but then it just fires the same event again. I tried putting an instance variable in there to prevent the code from getting executed again, but it doesn't seem to work.
There should be an elegant, simple way to keep the user from tabbing off if there is a data integrity issue at stake. How can we ensure that the current tab (or the previous tab at this point in the process) is forced to remain as the highlighted tab?
The xamTabControl is a derived Selector and Selector does not offer an event before the selection takes place so there really isn't a good way to prevent the selection. While it may be possible to try and handle something like the PreviewLostKeyboardFocus event, that event is likely to be raised multiple times so you would probably end up with multiple messageboxes for the same change.