RadListBox - ContextMenu for an item

4 posts, 0 answers
  1. shay
    shay avatar
    16 posts
    Member since:
    Jun 2015

    Posted 18 Jan 2016 Link to this post


    I am using RadListBox with ContextMenu  for an item,

    on every right click (only on an item) i need

    1. that the menu will be open only on the item (when i am clicking on free space on the list area, i don't want that the menu will open)

    2. get the selected item to my viewModel


    this is my code:


               ItemSource ="{Binding collection}" , SelectedItem="{Binding Selected2, mode =TwoWay}">



              <telerik:RadMenuItem Header="This menu is only for item" command="{Binding c}" CommandParamter="{Binding=selectedItem}"/>






  2. Geri
    Geri avatar
    62 posts

    Posted 20 Jan 2016 Link to this post

    Hi Shay,

    You can achieve the desired by adding the context menu through a style, that is set to the ItemContainerStyle property of RadListBox. We suppose the free space you talk about appears on the bottom of the control, after the last item. In the code you provided, there is no Height set, but in case it is set elsewhere, you can try to set only MaxHeight to RadListBox - this way, the control will be stretched according to the number of items, but will not exceed the set value.

    The following code demonstrates all of the above mentioned:

        <telerik:RadContextMenu x:Key="menu">
                <telerik:RadMenuItem Header="This menu is only for item" x:Name="radMenuItem"
                                     Command="{Binding c}" CommandParameter="{Binding selectedItem}"/>
        <Style TargetType="telerik:RadListBoxItem" x:Key="itemStyle">
            <Setter Property="telerik:RadContextMenu.ContextMenu" Value="{StaticResource menu}"/>
        <telerik:RadListBox x:Name="listBox" ItemsSource="{Binding collection}" SelectedItem="{Binding Selected2, Mode=TwoWay}"
                            ItemContainerStyle="{StaticResource itemStyle}" MaxHeight="100"/>

    In your code, it appears that the selected item of RadListBox is bound to a property in the ViewModel, so it should be updated accordingly without any problems.

    Hope this helps.

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. shay
    shay avatar
    16 posts
    Member since:
    Jun 2015

    Posted 24 Jan 2016 in reply to Geri Link to this post

    Thanks for the answer :)

    Now the context menu is only on the items (as i want)

    but i didn't reach my "command" code in my viewModel. 

    this is my code after your help:

         <telerik:RadContextMenu x:Key="menu">
                <telerik:RadMenuItem Header="This menu is only for item" x:Name="radMenuItem"
                                     Command="{Binding c}" CommandParameter="{Binding selectedItem}"/>
          <Style TargetType="telerik:RadListBoxItem" x:Key="itemStyle">
               <Setter Property="telerik:RadContextMenu.ContextMenu" Value="{StaticResource menu}"/>


               ItemSource ="{Binding collection}" , SelectedItem="{Binding SelectedItem2, mode =TwoWay}"

               ItemContainerStyle="{StaticResource itemStyle}" 


  4. shay
    shay avatar
    16 posts
    Member since:
    Jun 2015

    Posted 24 Jan 2016 in reply to shay Link to this post

    My mistake. it's works :) thanks
Back to Top