locked
How can I determine the Datepicker (parent) of a datapickertextbox RRS feed

  • Question

  • I need to retrieve the name of the datepicker control that is the parent of a of datepickertextbox, is this possible?

    Sunday, May 29, 2011 8:30 PM

Answers

  • Hi 196MBVNS84,

    First of all, you can add a loaded or any other event handler of DatePickerTextBox to get DatePickerTextBox from event sender. Then you can use VisualTreeHelper and recursive method to get DatePickerTextBox's parent until find the DatePicker.

            private void TextBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
            {
                TextBox tb = sender as TextBox;
                DatePicker dp = this.GetParentByType(tb) as DatePicker;
                if (dp != null)
                {
                    dp.Width = dp.Width + 100;
                }
    
            }
            private FrameworkElement GetParentByType(FrameworkElement element)
            {
                FrameworkElement parent = VisualTreeHelper.GetParent(element) as FrameworkElement;
                if (parent != null)
                {
                    if (parent is DatePicker)
                    {
                        return parent;
                    }
                    return GetParentByType(parent);
                }
                return null;
            }

    http://forums.silverlight.net/forums/p/19912/68700.aspx

    Best Regards,
     

    Thursday, June 2, 2011 11:47 AM

All replies

  • Monday, May 30, 2011 3:27 AM
  • Hi 196MBVNS84,

    First of all, you can add a loaded or any other event handler of DatePickerTextBox to get DatePickerTextBox from event sender. Then you can use VisualTreeHelper and recursive method to get DatePickerTextBox's parent until find the DatePicker.

            private void TextBox_Loaded(object sender, System.Windows.RoutedEventArgs e)
            {
                TextBox tb = sender as TextBox;
                DatePicker dp = this.GetParentByType(tb) as DatePicker;
                if (dp != null)
                {
                    dp.Width = dp.Width + 100;
                }
    
            }
            private FrameworkElement GetParentByType(FrameworkElement element)
            {
                FrameworkElement parent = VisualTreeHelper.GetParent(element) as FrameworkElement;
                if (parent != null)
                {
                    if (parent is DatePicker)
                    {
                        return parent;
                    }
                    return GetParentByType(parent);
                }
                return null;
            }

    http://forums.silverlight.net/forums/p/19912/68700.aspx

    Best Regards,
     

    Thursday, June 2, 2011 11:47 AM