开发者

Creating ContextMenu in XAML, and adding it to an Item in Code?

I'm using the ContextMenu control from the Silverlight Toolkit, and I'd li开发者_高级运维ke to define the contextmenu in xaml, and "add it" to a control that I create in code.

I'm using the Telerik Outlook Radbar, that contains the Telerik RadTreeView. I'm currently creating the RadTreeView in code via a Converter class, but I'd like each treeviewitem to have a right-click menu.

Is there a way to define the ContextMenu as a DataTemplate, and set the RadTreeView to use that?

This is what I have, but it doesn't work as I expect (I get the default silverlight right-click menu)

XAML

<DataTemplate x:Key="TreeViewItemTemplate">
    <controlsToolkit:ContextMenuService.ContextMenu>
       <controlsToolkit:ContextMenu>
          <controlsToolkit:MenuItem Header="New Folder" 
                                    Click="NewFolderMenu_Click"/>
       </controlsToolkit:ContextMenu>
    </controlsToolkit:ContextMenuService.ContextMenu>
</DataTemplate>

CS

var t = new RadTreeView();

t.IsLineEnabled = true;
t.IsDragDropEnabled = true;
t.ItemTemplate = (DataTemplate) MailboxView.MailboxResources["TreeViewItemTemplate"];


ContextMenu is an attached property of the ContextMenuService class, and the menu will be applied to the element, to which this property is attached. In your example it is attached to the empty data template, which is not represented by anything in the UI, so you have nothing to right-click on to get your menu. Attaching this property to something visible will fix the issue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜