SilverlightShow: Anatomy of the ContextMenu: how it is made, how to use it and how to abuse it Comments http://www.silverlightshow.net/ Silverlight articles, Silverlight tutorials, Silverlight videos, Silverlight samples en-us SilverlightShow.net estoychev@completit.com (Emil Stoychev) Argotic Syndication Framework, http://www.codeplex.com/Argotic http://www.rssboard.org/rss-specification RE: Anatomy of the ContextMenu: how it is made, how to use it and how to abuse it http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx#comment3680 <p>Hi Walter, thanks for the article.</p> <p>It helped me to understand how ContextMenu works, but it didn't help me to solve my problem.</p> <p>I need to bind ContextMenu items dynamically, this is my XAML:</p> <span style="font-family: consolas; color: #a31515; font-size: 13px;"><span style="font-family: consolas; color: #a31515; font-size: 13px;"><span style="font-family: consolas; color: #a31515; font-size: 13px;"> <p> </p> </span> <p> </p> </span> <p> </p> </span> <p><span style="font-family: consolas; color: #0000ff; font-size: 13px;"><</span><span style="font-family: consolas; color: #a31515; font-size: 13px;">toolkit</span><span style="font-family: consolas; color: #0000ff; font-size: 13px;">:</span><span style="font-family: consolas; color: #a31515; font-size: 13px;">ContextMenu</span><span style="font-family: consolas; color: #ff0000; font-size: 13px;"> ItemsSource</span><span style="font-family: consolas; color: #0000ff; font-size: 13px;">="{</span><span style="font-family: consolas; color: #a31515; font-size: 13px;">Binding</span><span style="font-family: consolas; color: #ff0000; font-size: 13px;"> ...</span><span style="font-family: consolas; color: #0000ff; font-size: 13px;">}"></span></p> <p><span style="font-family: consolas; font-size: 13px;"><span style="color: #0000ff;"><</span><span style="color: #a31515;">toolkit</span><span style="color: #0000ff;">:</span><span style="color: #a31515;">ContextMenu.ItemTemplate</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"><span style="color: #0000ff;"><span style="font-family: consolas; font-size: 13px;">></span> <p> </p> <p> </p> </span> <p> </p> </span> <p> </p> </span></p> <p><span style="font-family: consolas;"><span style="color: #0000ff; font-size: 13px;"><</span><span style="color: #a31515; font-size: 13px;">DataTemplate</span></span><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="font-family: consolas;">></span> <p> </p> <p> </p> </span> <p> </p> </span> <p> </p> </span></p> <p><span style="font-family: consolas;"><span style="color: #0000ff; font-size: 13px;"><</span><span style="color: #a31515; font-size: 13px;">toolkit</span><span style="color: #0000ff; font-size: 13px;">:</span><span style="color: #a31515; font-size: 13px;">MenuItem</span><span style="color: #ff0000; font-size: 13px;"> Header</span><span style="color: #0000ff; font-size: 13px;">="{</span><span style="color: #a31515; font-size: 13px;">Binding</span><span style="color: #ff0000; font-size: 13px;"> ...</span><span style="color: #0000ff; font-size: 13px;">}"</span></span><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="font-family: consolas;">></span> <p> </p> <p> </p> </span> <p> </p> </span> <p> </p> </span></p> <p><span style="font-family: consolas;"><span style="color: #0000ff; font-size: 13px;"><</span><span style="color: #a31515; font-size: 13px;">toolkit</span><span style="color: #0000ff; font-size: 13px;">:</span><span style="color: #a31515; font-size: 13px;">MenuItem.Icon</span></span><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="font-family: consolas;">></span> <p> </p> <p> </p> </span> <p> </p> </span> <p> </p> </span></p> <p><span style="font-family: consolas;"><span style="color: #0000ff; font-size: 13px;"><</span><span style="color: #a31515; font-size: 13px;">Image</span><span style="color: #ff0000; font-size: 13px;"> Source</span><span style="color: #0000ff; font-size: 13px;">="{</span><span style="color: #a31515; font-size: 13px;">Binding</span><span style="color: #ff0000; font-size: 13px;"> ...</span><span style="color: #0000ff; font-size: 13px;">}"</span></span><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="font-family: consolas;">/></span> <p> </p> <p> </p> </span> <p> </p> </span> <p> </p> </span></p> <p><span style="font-family: consolas;"><span style="color: #0000ff; font-size: 13px;"></</span><span style="color: #a31515; font-size: 13px;">toolkit</span><span style="color: #0000ff; font-size: 13px;">:</span><span style="color: #a31515; font-size: 13px;">MenuItem.Icon</span></span><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="font-family: consolas;">></span> <p> </p> <p> </p> </span> <p> </p> </span> <p> </p> </span></p> <p><span style="font-family: consolas;"><span style="color: #0000ff; font-size: 13px;"></</span><span style="color: #a31515; font-size: 13px;">toolkit</span><span style="color: #0000ff; font-size: 13px;">:</span><span style="color: #a31515; font-size: 13px;">MenuItem</span></span><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="font-family: consolas;">></span> <p> </p> <p> </p> </span> <p> </p> </span> <p> </p> </span></p> <p><span style="font-family: consolas;"><span style="color: #0000ff; font-size: 13px;"></</span><span style="color: #a31515; font-size: 13px;">DataTemplate</span></span><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="font-family: consolas;">></span> <p> </p> <p> </p> </span> <p> </p> </span> <p> </p> </span></p> <p><span style="font-family: consolas;"><span style="color: #0000ff; font-size: 13px;"></</span><span style="color: #a31515; font-size: 13px;">toolkit</span><span style="color: #0000ff; font-size: 13px;">:</span><span style="color: #a31515; font-size: 13px;">ContextMenu.ItemTemplate</span><span style="color: #0000ff; font-size: 13px;">></span></span><span style="color: #a31515; font-size: 13px;"><span style="color: #a31515; font-size: 13px;"><span style="color: #a31515; font-size: 13px;"><span style="font-family: consolas;"> </span> <p> </p> <p> </p> </span> <p> </p> </span> <p> </p> </span></p> <p><span style="font-family: consolas;"><span style="color: #0000ff; font-size: 13px;"></</span><span style="color: #a31515; font-size: 13px;">toolkit</span><span style="color: #0000ff; font-size: 13px;">:</span><span style="color: #a31515; font-size: 13px;">ContextMenu</span></span><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="color: #0000ff; font-size: 13px;"><span style="font-family: consolas;">></span> <p> </p> <p>The databinding works as expected, but the icons are displayed on the right side of the separator with the rest of the menuitem... Any ideas?</p> </span> <p> </p> </span> <p> </p> </span></p> <p> </p> ( J) http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx Fri, 28 May 2010 18:04:03 +0300 RE: Anatomy of the ContextMenu: how it is made, how to use it and how to abuse it http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx#comment3618 <p>Hi Jon,</p> <p>no it is not possible to use exactly the same approach of WPF. As you can read in the <a href="http://blogs.msdn.com/delay/archive/2010/04/15/alive-and-kickin-new-silverlight-4-toolkit-released-with-today-s-silverlight-4-rtw.aspx">Delay's Blog</a> :  " Silverlight 4 doesn't have the <a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.contextmenu.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.contextmenu.aspx">FrameworkElement.ContextMenu</a> property that's often used to attach a <code>ContextMenu</code> to an element. Instead, it is always necessary to use the (functionally equivalent and more flexible) <code>ContextMenuService.ContextMenu</code> attached property like I show above".</p> <p> </p> ( walterf) http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx Sat, 15 May 2010 09:25:33 +0300 RE: Anatomy of the ContextMenu: how it is made, how to use it and how to abuse it http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx#comment3616 <p>I'm wondering why this works differently than it does in WPF. Specifically, I'm wondering why you can't seem to just do the following:</p> <p></p>     <Grid><br />         <Grid.ContextMenu><br />             <ContextMenu><br />                 ...<br />             </ContextMenu><br />         </Grid.ContextMenu><br />     </Grid><br /> <p> i.e. the ContextMenu property is missing.</p> <p></p> ( Jon) http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx Sat, 15 May 2010 02:15:44 +0300 RE: Anatomy of the ContextMenu: how it is made, how to use it and how to abuse it http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx#comment3606 <p>If you want support for submenus uploaded a class that adds support for it on Codeplex:</p> <p><a href="http://sl4popupmenu.codeplex.com/">http://sl4popupmenu.codeplex.com/</a></p> <p>The project is still in beta version and unfortunately it is not based on the new ContextMenu Control introduced with the latest release of the Silverlight toolkit but it is a quite a decent altenative if you badly need support for submenus.</p> ( Ziad JKhan) http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx Thu, 13 May 2010 11:53:18 +0300 RE: Anatomy of the ContextMenu: how it is made, how to use it and how to abuse it http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx#comment3543 <p>Hi Simon,<br /> some more details?  I just tried in a trivial example and it seems to work. </p> <p> </p> ( walterf) http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx Thu, 06 May 2010 10:01:31 +0300 RE: Anatomy of the ContextMenu: how it is made, how to use it and how to abuse it http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx#comment3542 i don't want to abuse the control - I just want to use it on a DataGrid and I'm having a really bad time getting this to work!! any tips? ( simon) http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx Thu, 06 May 2010 08:16:00 +0300 RE: Anatomy of the ContextMenu: how it is made, how to use it and how to abuse it http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx#comment3537 Thanks, this might be an idea.. :-) ( walterf) http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx Wed, 05 May 2010 18:55:56 +0300 RE: Anatomy of the ContextMenu: how it is made, how to use it and how to abuse it http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx#comment3535 Thats awesome, You should a sequence of post intitled "The Anatomy Of <Control here>". The hardest part of Silverlight and WPF are in understanding the structure of controls! <div><br /> </div> <div>Tks...</div> ( Marcelo) http://www.silverlightshow.net/items/Anatomy-of-the-ContextMenu-how-it-is-made-how-to-use-it-and-how-to-abuse-it.aspx Wed, 05 May 2010 15:25:54 +0300