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

RadListBox - ContextMenu for an item

3 Answers 502 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
shay
Top achievements
Rank 1
shay asked on 18 Jan 2016, 07:24 AM

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>

 

 

 

 

3 Answers, 1 is accepted

Sort by
0
Geri
Telerik team
answered on 20 Jan 2016, 01:42 PM
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
0
shay
Top achievements
Rank 1
answered on 24 Jan 2016, 02:30 PM

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>

0
shay
Top achievements
Rank 1
answered on 24 Jan 2016, 03:40 PM
My mistake. it's works :) thanks
Tags
ListBox
Asked by
shay
Top achievements
Rank 1
Answers by
Geri
Telerik team
shay
Top achievements
Rank 1
Share this question
or