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

Show/Hide popup menu dynamically based on item count

3 Answers 133 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Marc
Top achievements
Rank 1
Marc asked on 24 Jan 2012, 05:00 PM
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

3 Answers, 1 is accepted

Sort by
0
Konstantina
Telerik team
answered on 27 Jan 2012, 04:45 PM
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 >>
0
Marc
Top achievements
Rank 1
answered on 27 Jan 2012, 04:53 PM
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
0
Konstantina
Telerik team
answered on 02 Feb 2012, 09:17 AM
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 >>
Tags
Menu
Asked by
Marc
Top achievements
Rank 1
Answers by
Konstantina
Telerik team
Marc
Top achievements
Rank 1
Share this question
or