Split Button in WPF

One of the commonly requested controls in WPF is the split button... its pretty simple to implement it in WPF... heres my take on it with a slight twist... ;)

In the control template of the button, I have the following - the TextBlock and the Menu being the additions..

<ContentPresenter .../>
<TextBlock Grid.Row="0" Grid.Column="1" Text="|" Foreground="gray" />
<Menu Grid.Row="0" Grid.Column="2" Background="transparent" >
<MenuItem Header=">" Margin="0" Padding="0" Name="DropDownButton"/>
</Menu>

Thats it with the style .. I tend to shy away from manipulating styles .. :) ... so now for the menu I add items to the context menu of the button ...

 <Button.ContextMenu>
<ContextMenu >
<MenuItem Header="_Save" Command="Save"/>
<MenuItem Header="_Open" Command="Open"/>
</ContextMenu>
</Button.ContextMenu>

Next on loading we need to open the context menu on clicking the Menu in the button.. so we do that with this code snippet

b.ContextMenu.Width = b.ActualWidth; // context menu should be the same width as button
b.ContextMenuOpening += new ContextMenuEventHandler(b_ContextMenuOpening);
DependencyObject d = VisualTreeHelper.GetChild(b, 0); //this gives the buttonChrome
MenuItem m = LogicalTreeHelper.FindLogicalNode(d, "DropDownButton") as MenuItem;
m.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(m_PreviewMouseLeftButtonUp);

Now that we have the handler simply set the IsOpen property to true and also set the placement of the ContextMenu at the bottom

b.ContextMenu.PlacementTarget = b;
b.ContextMenu.Placement = System.Windows.Controls.Primitives.PlacementMode.Bottom;
ContextMenuService.SetPlacement(b, System.Windows.Controls.Primitives.PlacementMode.Bottom);
b.ContextMenu.IsOpen = true;

The last part is ensuring that the context menu is not opened when you right click on the button or through the keyboard ContextMenu key. This is acheived in the ContextMenuOpening handler.

b.ContextMenu.IsOpen = false;

The complete project is attached ...

SplitButton.zip