How to get the related RadDatePicker from Telerik.Windows.Controls.PickerTextBox

5 posts, 1 answers
  1. Kevin
    Kevin avatar
    9 posts
    Member since:
    Mar 2009

    Posted 21 Nov 2009 Link to this post

    I have two controls which are TextBox and RadDatePicker. Initially i have a focus on TextBox and then I click on RadDatePicker's edit field. During the lost focus event of TextBox I call FocusManager.GetFocusedElement() to find out which control got the focus.

    The control is returned as Telerik.Windows.Controls.PickerTextBox. My question is how I can find out whick RadDatePicker control related to PickerTextBox.
  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 23 Nov 2009 Link to this post

    Hi Kevin,

    Why do you need to know which element got focus?
    Could you tell us what is your requirement?

    Kind regards,
    Hristo
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. DevCraft banner
  4. Kevin
    Kevin avatar
    9 posts
    Member since:
    Mar 2009

    Posted 23 Nov 2009 Link to this post

    Hi Hristo,
     
        When I received the lost focus event of first TextBox, i am doing some validation for the data I got from TextBox. If the data is invalid, I need to set the focus back to TextBox control.

     

        Since user click DatePicker control to cause TextBox to lost focus, the DatePicker will fire the lost focus event to do some validation when I set the focus back to TextBox. As a result, i need to skip the validation on DatePicker in this case.

     

        As a result, after I get the focus control, I will know which control's validation event i need to skip.

     

        Above case is a simple case. In real project there is a generic function to handle the validation logic.

    Thanks!

    -Kevin

  5. Answer
    Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 25 Nov 2009 Link to this post

    Hello Kevin,

    In WPF there is a TemplatedParent property that can be used for the purpose but in Silverlight it is internal.
    I've created small class that will help you get the templated parent of some control.

    Here is the helper class:
    public class TemplatedParentHelper
    {
        private static Control GetTemplatedParentInternal(DependencyObject obj)
        {
            return (Control)obj.GetValue(TemplatedParentInternalProperty);
        }
      
        private static readonly DependencyProperty TemplatedParentInternalProperty =
            DependencyProperty.RegisterAttached("TemplatedParentInternal", typeof(Control), typeof(TemplatedParentHelper), new PropertyMetadata(null));
      
        public static Control GetTemplatedParent(Control control)
        {
            Binding bindingHelper = new Binding() { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) };
            control.SetBinding(TemplatedParentInternalProperty, bindingHelper);
            Control templatedParent = GetTemplatedParentInternal(control);
            control.ClearValue(TemplatedParentInternalProperty);
            return templatedParent;
        }
    }

    And here is the usage:
    void MainPage_LostFocus(object sender, RoutedEventArgs e)
    {
        Control focusedElement = System.Windows.Input.FocusManager.GetFocusedElement() as Control;
        RadDatePicker datePicker = TemplatedParentHelper.GetTemplatedParent(focusedElement) as RadDatePicker;
        if (datePicker != null)
        {
            // Do validation.
        }
    }

    I hope that this will help you.
    Let us know if you need more information.

    Best wishes,
    Hristo
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  6. Dondre
    Dondre avatar
    1 posts
    Member since:
    Jul 2012

    Posted 23 May 2013 Link to this post

    Hi,

    I see this is a kind of an old post but to directly answer the posters question you'd used the following:

    C#
    var datePicker = ((PickerTextBox)FocusManager.GetFocusedElement).GetParents()[1] as RadDatePicker;

    VB.NET
    Dim datePicker As RadDatePicker = TryCast(DirectCast(FocusManager.GetFocusedElement, Telerik.Windows.Controls.PickerTextBox).GetParents()(1), RadDatePicker)

Back to Top
DevCraft banner