Can't find what you're looking for?

Contact us or post in our forums and we'll help you find an answer.

How to Set Focus on DateTimeEditors in WPF Edition

Background:

Invoking Focus method does not work for ComponentOne’s DateTimeEditors controls like C1DatePicker/C1DateTimePicker. This happens because C1DatePicker/C1DateTimePicker is composed of many controls hence invoking Focus method does not effectively focuses the control.

Steps to Complete:

There are two ways to set focus on DateTimeEditors in WPF Edition:

  • Using VisualTreeHelper: Find internal C1MaskedTextBox using VisualTreeHelper and focus it at runtime.

var grid = VisualTreeHelper.GetChild(_datepicker1, 0) as Grid;
var innerGrid = grid.Children[1] as Grid;
var txtbox = innerGrid.Children[2] as C1MaskedTextBox;
txtbox.Focus();
  • Creating custom control: Create custom C1DatePicker/C1DateTimePicker inheriting C1DatePicker/C1DateTimePicker respectively and override its OnGotFocus method.

public class CustomC1DatePicker : C1DatePicker
{
    protected override void OnGotFocus(RoutedEventArgs e)
    {
        base.OnGotFocus(e);
        var txtbox = this.GetTemplateChild("TextBox") as C1MaskedTextBox;
        txtbox.Focus();
    }
}

Get the Latest Version of WPF Edition
Download the Free Trial Now