This is a migrated thread and some comments may be shown as answers.

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

4 Answers 154 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 21 Nov 2009, 11:06 AM
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.

4 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 23 Nov 2009, 11:02 AM
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.
0
Kevin
Top achievements
Rank 1
answered on 24 Nov 2009, 01:07 AM

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

0
Accepted
Hristo
Telerik team
answered on 25 Nov 2009, 02:36 PM
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.
0
Dondre
Top achievements
Rank 1
answered on 24 May 2013, 02:10 AM
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)

Tags
DatePicker
Asked by
Kevin
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Kevin
Top achievements
Rank 1
Dondre
Top achievements
Rank 1
Share this question
or