Condividi tramite


Banana SplitButton [A WPF-specific fix for SplitButton and some code analysis improvements for the Silverlight version, too]

**

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

  • Anonymous
    July 06, 2010
    i have achieved these functionalities by using a ComboBox. i have edited its default template and added couple of properties. but the logic/events/data is already inside the ComboBox, so why duplicate? the only reason i think of (using the ContextMenu) is if in the future it will have sub-menus. otherwise... it is much simpler modifying the ComboBox.

  • Anonymous
    July 07, 2010
    Shemesh, I started SplitButton as a way to test my ContextMenu implementation on Silverlight, so using ComboBox wasn't something I really considered. :) That said, having Button as the base class seems slightly preferable to me - as does using an actual Menu subclass for the menu. But it's cool to hear this can be done simply with ComboBox and if that's working well for you, it's great to hear! Thanks for sharing!

  • Anonymous
    July 07, 2010
    hmmmm, got it. i was looking at it from the functionality point of view. you were coming from a different one. my work was done from up downwards, yours was done from down upwards. :-)

  • Anonymous
    July 13, 2010
    Vote for Menu & MenuButton to be added to the toolkit

  • Anonymous
    July 14, 2010
    KierenH, Noted, thanks! :)

  • Anonymous
    September 16, 2010
    I created another version of this control called SelectButton http://gist.github.com/580405

  • Anonymous
    November 22, 2010
    The comment has been removed

  • Anonymous
    November 22, 2010
    jpierson, The comment for the code you're referring to is: "// Add the ContextMenu as a logical child (for DataContext and RoutedCommands)". It's WPF-only and if that scenario's not relevant to you, you might consider removing that block entirely. As you say, though, that's not a general-purpose solution - at the time I was porting SplitButton to WPF, what I implemented there worked well in the scenarios I tried - but if you find a better solution, I'd love to hear it! Regarding getting the latest version of stuff I post to my blog, I almost always point to a single location for my ZIP downloads and update that ZIP as I make improvements over time. So clicking the download link should always get you the latest bits. :) I've considered moving to something like CodePlex, but haven't made the plunge yet (there are actually rules for using CodePlex as a Microsoft employee that might prevent me from using it for the purpose you suggest). If I do switch to something like CodePlex or GitHub, I'll be sure to post about it. Thanks for your note - and good luck finding a better solution!

  • Anonymous
    November 22, 2010
    Delay, Thanks for your input. In my case I'm actually using your control in a WPF application not Silverlight and that's why I noticed this issue, I should have mentioned that at first. Unfortunately I haven't found an alternative way to get the ContextMenu's Popup to be set as a Logical Child of the MenuButton. I've tried setting the Opacity  to 0 and also the Height and Width to 0 to try to get the ContextMenu to not show during the open/close logic in ApplyTemplate but that doesn't seem to have any effect. I have found by looking at the ContextMenu's code through Reflector that there is a method called HookupParentPopup() that seems to be exactly what is needed but unfortunately it's private.... Right now I'm wondering how this works for regular contextmenus on controls set through the ContextMenu attached property.

  • Anonymous
    November 22, 2010
    The comment has been removed

  • Anonymous
    November 22, 2010
    jpierson, Great stuff - thank you very much for that detailed write-up! I don't have specific plans to update SplitButton right now, but I've added a note to my TODO list referencing the issue you report and your proposed fix. For what it's worth, I'm also not a big fan of private reflection - but maybe when I (or you) revisit this at some future date, an alternate solution will present itself... :)

  • Anonymous
    December 14, 2010
    I've just run into another issue this time with RelativeSource binding defined on properties of the MenuItems used in my sample application. In my case I need to access a property on one of the parent controls such as a Window or UserControl. To bind to the property I'm using a binding that looks like the example below. <MenuItem Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Background}" /> In my real project I'm binding something a little more specific than just the Background of the Window but the point is still the same. Also I'm not sure whether this is a problem with SplitButton implementation yet or just something that would occur with all bindings specified to items within a ContextMenu. If I find any more details I'll follow up here to help others that may need to do something similar.

  • Anonymous
    December 14, 2010
    jpierson, If I had to guess at why RelativeSource wouldn't work here, my first thought would be that it might be due to the ContextMenu-imposed Popup sitting between the MenuItems and the rest of the page. If so, one possible workaround might be to use something like "{Binding ElementName=MyWindowName, Path=Background}" instead - though that might suffer from the same root problem... Big thanks for the detailed information and for sharing your findings!

  • Anonymous
    July 04, 2011
    I just implemented the buttons in my project. Works like a treat! Thank you very much for publishing this.

  • Anonymous
    July 04, 2011
    Helge Klein, Glad it worked for you - thanks for the message! :)

  • Anonymous
    August 08, 2011
    Hi David, Don't understand why you are adding the contextmenu as a logical child. I just put it in the button context Menu and it works : this.ContextMenu = _contextMenu;

  • Anonymous
    August 08, 2011
    Humm in fact it didn't work. I'm trying to understand because the context menu appear in the window while the code is parsing the logical tree wich is not really nice.

  • Anonymous
    August 09, 2011
    Aleks, I explain why the ContextMenu is set as a logical child in the second paragraph of the post above. As you've also discovered, certain things (ex: DataContext, RoutedCommands) don't work otherwise. There may be another way to get this all working, but that what I found worked at the time. :)

  • Anonymous
    April 03, 2012
    Hi Kainhart, In addition to my previous question, I found a workaround for context menu showing when loading the SplitButton, with this workaround the Command binding will still work when using the SplitButton on a DataTemplate: On the OnApplyTemplate hide the contextmenu: _contextMenu.Visibility = System.Windows.Visibility.Hidden; On OpenButtonMenu make the contextmenu visible again: _contextMenu.Visibility = System.Windows.Visibility.Visible;

  • Anonymous
    December 06, 2013
    Unfortunately opening a popup and adding it as logical child has huge impact on performance. i tried putting SplitButton as a grid cell and it gave up pretty quickly. Somehow all the things you attempted to solve (RoutedCommands, DataContext) work in ComboBox which also does use Popup (ContextMenu does use popup).