I have a TabControl and I'm trying to allow the user to change the tab name... but only when the name is double-clicked. That way, users can click the different tab names to simply change the active tab, but also change the tab name if they desire.
What I've tried so far is to capture the MouseDoubleClick and LostFocus events, then set the "Focusable" property to be true only when the tab name is double clicked. The problem with this method is the LostFocus event is firing immediately after the double click, presumably because the focus is being set to the content of the TabItem.
My tab control XAML:
<Mah:MetroAnimatedTabControl x:Name="ViewTabs" DataContext="{Binding MyTabsViewModel}" ItemsSource="{Binding}">
<Mah:MetroAnimatedTabControl.ItemTemplate>
<DataTemplate DataType="{x:Type viewModels:MyTabViewModel}">
<TextBox x:Name="TabNameTextBox"
Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
MouseDoubleClick="TabNameTextBox_MouseDoubleClick"
LostFocus="TabNameTextBox_LostFocus"
Cursor ="Arrow"/>
</DataTemplate>
</Mah:MetroAnimatedTabControl.ItemTemplate>
</Mah:MetroAnimatedTabControl>
Code behind events MouseDoubleClick and Lost Focus:
private void TabNameTextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var textBox = (TextBox)sender;
textBox.Focusable = true;
textBox.Focus();
textBox.SelectAll();
}
private void TabNameTextBox_LostFocus(object sender, RoutedEventArgs e)
{
var textBox = (TextBox)sender;
textBox.Focusable = false;
}
I found a similar question where the asker couldn't get the Lost Focus event to fire. In my situation, it is firing before I expect it to.
