Show/Hide popup menu dynamically based on item count

4 posts, 0 answers
  1. Marc
    Marc avatar
    88 posts
    Member since:
    Jul 2011

    Posted 24 Jan 2012 Link to this post

    I've got a RadMenuItem bound to a list of items which has always at least one item. What I need to do is to hide the popup in case the list has exactly one item. In all other cases I want to show it.

    Since I need to customize the RadMenuItems control template anyway, I tried to solve this problem via binding in the control template.

    First I've bound the popup element's visibility against the observable item collection with a converter in between that returned "Collapsed" when the count was less than 2. However this didn't work because my popup is wrapped in a Telerik specific PopupWrapper which is still visible then.

    So I tried to bind the popup's horizontal offset with a converter that moves the whole stuff out of view if necessary (see snippet below). This doesn't work either because horizontal offset is just read once when creating the PopupWrapper. However, when my items collections changes dynamically from 2 items to 1, my popup element gets the desired negative offset but not the wrapper. In the end the popup is still visible. So this does not work either.

    <ControlTemplate x:Key="TopMenuItemControlTemplate">
    ...
    <Telerik:Popup
    x:Name="PART_Popup"
    HorizontalOffset="-6"
    VerticalOffset="{Binding Items.Count, Converter={StaticResource CountToOffsetConverter}}">
    <Grid>
    ...
    </Grid>
    </Telerik:Popup>
    </ControlTemplate>
     
    <Telerik:RadMenuItem
      ItemsSource="{Binding Items}"
      Telerik:RadMenuItem.TopLevelHeaderTemplateKey="{StaticResource TopMenuItemControlTemplate}" />


    Any ideas on how to do this. I don't think I'm the first one with this scenario...

    Regards,
    Stephan
  2. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 27 Jan 2012 Link to this post

    Hi Stephan,

    I can suggest you the following:
    - hook to the Opening event of the RadContextMenu and check whether there is only one item in the collection. If the item is only one you could set the e.Handled=True and the menu won't open.

    Please let us know if that helped.

    Greetings,
    Konstantina
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  3. DevCraft banner
  4. Marc
    Marc avatar
    88 posts
    Member since:
    Jul 2011

    Posted 27 Jan 2012 Link to this post

    Thank you for your answer but I'm not talking about a RadCONTEXTMenu. I'm using a RadMenu with one RadMenuItem whose sub-items are displayed in a popup which in turn should only be opened if it has more than one item.

    <Telerik:RadMenu Background="Transparent" NotifyOnHeaderClick="True">
                <Telerik:RadMenuItem
                    Click="OnTopMenuItemClick"
                    ItemContainerStyle="{StaticResource SubMenuItemContainerStyle}"
                    ItemsSource="{Binding Source={StaticResource SubMenuItemsSource}}"
                    ItemTemplate="{StaticResource SubMenuItemDataTemplate}"
                    Telerik:RadMenuItem.TopLevelHeaderTemplateKey="{StaticResource TopMenuItemControlTemplate}" />
            </Telerik:RadMenu>

    Now, there is no event like "Opening" in RadMenu and in RadMenuItem there's only "SubmenuOpened" which is too late. What I actually needed was something like "SubmenuOpening".

    So, what is the best way to do this?

    Thank you,
    Stephan
  5. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 02 Feb 2012 Link to this post

    Hello Stephan,

    The control is not designed to work like this. The only thing I can think of is when the element is only one not the show the menu at all. If it changes dynamically to remove all the children and change the parent of the menu.

    Greetings,
    Konstantina
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Back to Top