Detecting the DropDownPosition

3 posts, 1 answers
  1. Jon
    Jon avatar
    10 posts
    Member since:
    Sep 2010

    Posted 03 Mar 2011 Link to this post

    Hi!

    Is it possible to detect the actual drop-down position of the DropDownContent?

    I'm trying to set the MaxHeight according to the total hight of the host (to avoid content beeing clipped when exceeding the boundaries of the browser, and insted get the vertical scroll-bars).

    Currently I'm doing this by measuring how far down the DropDown control is on the page. For example:

    Point

     

     

    dropDownPos = ddDropDown.TransformToVisual(null).Transform(new Point());

    double DropDownMaxHeight = Application.Current.Host.Content.ActualHeight - (dropDownPos.Y + ddDropDown.ActualHeight);


    This works fine, until the DropDownplacement changes from bottom to top (because we get further down on the page).

    So I need a way to detect when the control chooses to display the dropdown contents above instead of below the control, so that I can calculate the proper MaxHeight of the dropdown. (And the "auto-positioning" feature is just fine, I don't want to force the dropdown above or below)

    Currently, I'm adjusting the MaxHeight in the DropDownOpened event.

    Thanks!

    Jon

     

     

  2. Answer
    Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 07 Mar 2011 Link to this post

    Hello Jon,

    The automatic Position behavior that you describe happens when the popup encounters the edge of the screen. You can check the VerticalOffset of the popup to check whether the popup is positioned above or bellow its target:
    verticalOffset = (sender as RadDropDownButton).FindChildByType<Popup>().RealPopup.VerticalOffset;
    if(verticalOffset >0)
    {
        //the popup is opened bellow its placement target
    }
    else
    {
        //the popup is opened above its placement target
    }

    The (sender as RadDropDownButton).FindChildByType<Popup>() returns the Telerik Popup control wich is defined in the RadDropDownButton's Controltemplate, whilst its RealPopup property holds the System.Windows.Controls.Primitives.Popup control on which the Telerik's Popup control is cased on.

    Also, since it may take a moment for the popup to update its VerticalOffset value, it is best to place your logic inside a Dispatcher in order to make sure that you are working with the updated current values.

    I hope that info will help you.

    Regards,
    Tina Stancheva
    the Telerik team
    Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
  3. DevCraft banner
  4. Jon
    Jon avatar
    10 posts
    Member since:
    Sep 2010

    Posted 08 Mar 2011 Link to this post

    Hi Tina,

    Thanks, that did the trick!
    And yes, the Dispatcher is necessary to get the updated values.

    For the record, here's my complete code (notice that I have to remove the RealPopup property when in Silverlight.)

     

    private void ddDropDown_DropDownOpened(object sender, RoutedEventArgs e)
    {
        ddDropDown.Dispatcher.BeginInvoke(
            delegate()
            {
                double verticalOffset = (sender as RadDropDownButton).FindChildByType<Popup>().VerticalOffset;
                if (verticalOffset > 0)
                {
                    //the popup is opened below its placement target
                }
                else
                {
                    //the popup is opened above its placement target
                }
            }
        );
    }


    Cheers,
    Jon
Back to Top