RadListBox - ContextMenu for an item

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

    Posted 18 Jan Link to this post

    Hi,

    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:

    <telerik:RadListBox

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

     <teletik:RadContextMenu.ContextMenu>

           <teletik:RadContextMenu>

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

       </teletik:RadContextMenu.ContextMenu>
     </teletik:RadContextMenu>

     

     

     

     

  2. Geri
    Admin
    Geri avatar
    60 posts

    Posted 20 Jan 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:

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

    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.

    Regards,
    Geri
    Telerik
    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. UI for WPF is Visual Studio 2017 Ready
  4. shay
    shay avatar
    15 posts
    Member since:
    Jun 2015

    Posted 24 Jan 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:

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

    <Grid>

    <telerik:RadListBox
               ItemSource ="{Binding collection}" , SelectedItem="{Binding SelectedItem2, mode =TwoWay}"

               ItemContainerStyle="{StaticResource itemStyle}" 

    </Grid>

  5. shay
    shay avatar
    15 posts
    Member since:
    Jun 2015

    Posted 24 Jan in reply to shay Link to this post

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