I'm working on a WPF Application using MVVM. I have a view with tabs, and a listbox (which is not part of the tabControl) which displays all the validation errors on the view (using INotifyDataErrorInfo interface).
What I want to achieve: Once I click on an item within the listbox, the focus goes to the tab with the corresponding control and set focus on that control. For example, I have a validation error for First Name, so when I click on the item within the listbox describing the validation error for the first name, I want to switch to the tab where the First Name textbox is located and set focus for that textbox. Now, I followed what is written here: Set focus on textbox in WPF from view model (C#)
especially Zamotic reply ( he had the same situation with tabs as I have).
Now, the described situation above works well for me with the textboxes: it indeed goes to the First Name textbox and sets the focus for it.
The problem is that I have a WPF custom control in one of my tabs, and when I click on a validation error within the listbox regarding this custom control, it indeed goes to the correct tab, but no focus is set to the custom control. The custom control consists of a textBox, and I want the focus to be set to this textbox.
So, how do I handle Focus in a custom control so it fits the describes requirement above?
Here is the code for my custom control (the part of the code where I try to handle the focus):
public class NumericUpDownControl : Control
{
static NumericUpDownControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDownControl), new FrameworkPropertyMetadata(typeof(NumericUpDownControl)));
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
AttachToVisualTree();
this.GotFocus += new RoutedEventHandler(GetFocusHandler);
this.LostFocus += new RoutedEventHandler(LostFocusHandler);
}
private void GetFocusHandler(object sender, RoutedEventArgs e)
{
TextBox.Focus();
this.Focusable = false;
}
private void LostFocusHandler(object sender, RoutedEventArgs e)
{
IncreaseButton.Focus();
}
Textbox is the one set in the custom control Themes folder in Generic.xaml
var textBox = GetTemplateChild("valueBox") as TextBox;
if (textBox != null)
{
TextBox = textBox;
So, how do I handle the focus to achieve what I have already acheived for textboxes?
Edit 1: By the way, IncreaseButton is another control (which is a button) that my custom control consists of. When my custom control goes out of focus, I want to draw the focus off the textbox, so I just set it for that mentioned button.
Edit 2: In my view, I have a tab with the mentioned custom control. In my view model, I have a property called
public bool IsAgeFocused
And in the View I've written the following:
<numeric:NumericUpDownControl
x:Name="numericUpDownBox"
uiExtension:FocusExtension.IsFocused="{Binding IsAgeFocused}"
Focus Extension is the same class (without any additional info) like in the link:
Set focus on textbox in WPF from view model (C#)
The version described in Zamotic post (with the visibility part).