Developer test case, customer win! [Using ContextMenu to implement SplitButton and MenuButton for Silverlight (or WPF)]


This blog has moved to a new location and comments have been disabled.

All old posts, new posts, and comments can be found on The blog of dlaa.me.

See you there!

Comments (10)
  1. myCollections says:

    Hello,

    Thanks for sharing this code.

    Im trying to apply this code to WPF 4 to use in my app.

    I receive this error message :

    Value cannot be null.

    Parameter name: otherVisual

    on this line :

    Point currentOffset = _contextMenu.TransformToVisual(null).Transform(new Point());

    any clue ?

    Thanks for your help.

    Jeff

  2. David Anson says:

    myCollections,

    According to the MSDN documentation, WPF doesn’t allow a null parameter to TransformToVisual:

    http://msdn.microsoft.com/en-us/library/system.windows.media.visual.transformtovisual.aspx

    An alternative would be to walk up the visual tree to find the top-most Parent and pass that instead – that should work on WPF as well as Silverlight. I’ve added a note to my TODO list to look into this, but please let me know if you give it a try and have any luck. 🙂

    Thanks!

  3. John says:

    Have you got my response?

  4. John says:

    Delay, what a great control, thanks!

    But the posistioning of the menu is not perfect is the browser's zoom is set other than 100% or if you use it in OOB mode and your desktop DPI is other than 100%.

    Do you have any hint how to fix this?

    Thanks,

    John

  5. David Anson says:

    John,

    Sorry about the trouble! As I mention above, SplitButton was mainly a test case. I tried to catch some of the obvious edge cases, but deliberately didn't do them all. 🙂 I think I actually knew about the browser zoom issue at the time, but didn't want to complicate things too much since we still hadn't shipped at that point and there was plenty else to do.

    I've added a note to my TODO list to look into this – perhaps as part of the work to address the WPF-specific issue another commenter raised!

    For the time being, you might find some inspiration for fixing or addressing this here:

    blogs.msdn.com/…/the-code-not-taken-comparing-two-ways-of-creating-a-full-size-popup-in-silverlight-then-picking-the-best.aspx

    Hope that helps!

  6. Thomas says:

    David,

    Great control, but hard to find … any chance you will post it on Codeplex?

    regards

  7. David Anson says:

    Thomas,

    Thanks! Some of my stuff ends up on CodePlex, but usually because someone else is willing to take care of the project creation/management/overhead. 🙂 If you're interested in doing this, contact me privately and we can chat!

  8. David Anson says:

    Follow-up: I addressed the issues raised in the comments above and posted the following: blogs.msdn.com/…/splitbuttoning-hairs-two-fixes-for-my-silverlight-splitbutton-menubutton-implementation-and-true-wpf-support.aspx

    (The link to the ZIP is the same for both posts, so downloading from here also gets you the latest code.)

  9. Fred says:

    This is a great control.

    How do I template the context menu item. Because I want to set the source through code like below

    eg.

    <pre>

    ObservableCollection<object> PredefinedColors = new ObservableCollection<object>

         {

            new MyColor{ColorName = "Aliceblue", ColorCode = 0xFFF0F8FF},

            new MyColor{ColorName = "AntiqueWhite", ColorCode = 0xFFFAEBD7},

            new MyColor{ColorName = "Aqua", ColorCode = 0xFF00FFFF},

            new MyColor{ColorName = "Aquamarine", ColorCode = 0xFF7FFFD4} }

    Mysplitcutton.ButtonMenuItemsSource = PredefinedColors ;

    <pre>

  10. David Anson says:

    Fred,

    Thanks! 🙂

    So if you look at the default Style/Template for SplitButton/MenuButton (in ThemesGeneric.xaml), you'll see a ContextMenu in there. You should therefore be able to provide a custom Template that includes any ContextMenu customizations you want to make.

    Hope this helps!

Comments are closed.

Skip to main content