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

Using ItemsSource and DataTemplate

1 Answer 387 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Rayne
Top achievements
Rank 1
Rayne asked on 02 Aug 2011, 05:40 PM
I have a drop down button that contains a context menu as the drop down content. I'm binding my ItemsSource of the ContextMenu to a collection of Items on my view model. Because it's inside a DropDownButton, I'm handling the ItemClick event to close the drop down. But I'd like to also use a command on my view model and pass the bound object as the parameter.
Right now, I'm using a DataTemplate, but then I get a menuItem inside a menuItem.
Here is my button and context menu code.
<telerik:RadDropDownButton Content="Assign" DockPanel.Dock="Right" Margin="3" Name="RolesMenu">
   <telerik:RadDropDownButton.DropDownContent>
      <telerik:RadContextMenu ItemsSource="{Binding RolesList}" ItemClick="RolesMenu_ItemClick">
         <telerik:RadContextMenu.ItemTemplate>
            <DataTemplate>
                <telerik:RadMenuItem Header="{Binding RoleName}" Command="{Binding AssignRole}" CommandParameter="{Binding DataContext, RelativeSource={RelativeSource Mode=Self}}" />
            </DataTemplate>
         </telerik:RadContextMenu.ItemTemplate>
      </telerik:RadContextMenu>
   </telerik:RadDropDownButton.DropDownContent>
</telerik:RadDropDownButton>

How can I do the same thing as above without getting a menuItem inside a MenuItem?

1 Answer, 1 is accepted

Sort by
0
Accepted
Konstantina
Telerik team
answered on 05 Aug 2011, 12:06 PM
Hello Rayne,

In order the ContextMenu to work property you need to attach it like this:

<telerik:RadDropDownButton>
           <telerik:RadContextMenu.ContextMenu>
               <telerik:RadContextMenu>
               </telerik:RadContextMenu>
           </telerik:RadContextMenu.ContextMenu>
       </telerik:RadDropDownButton>

As explained in this help article. Otherwise it won't work correctly.
In order to achieve your goal you can use the ItemContainerStyle and through the Setter of the style to set the properties you need.
Attached I am sending you a sample project in which this is illustrated.

Hope this helps.

All the best,
Konstantina
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
ContextMenu
Asked by
Rayne
Top achievements
Rank 1
Answers by
Konstantina
Telerik team
Share this question
or