Skip to content

Instantly share code, notes, and snippets.

@rekkusu
Created May 16, 2010 12:36
Show Gist options
  • Save rekkusu/402854 to your computer and use it in GitHub Desktop.
Save rekkusu/402854 to your computer and use it in GitHub Desktop.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
namespace SleepyBird.View.Controls
{
/// <summary>
/// DropDownButton
/// Reference: http://akabeko.sakura.ne.jp/blog/2009/10/wpf-%E3%81%A7-dropdown-%E3%83%A1%E3%83%8B%E3%83%A5%E3%83%BC%E3%83%9C%E3%82%BF%E3%83%B3/
/// </summary>
public class DropDownButton : ToggleButton
{
public DropDownButton()
{
var binding = new Binding("DropDownContextMenu.IsOpen") { Source = this };
this.SetBinding(DropDownButton.IsCheckedProperty, binding);
}
public ContextMenu DropDownContextMenu
{
get
{
return this.GetValue(DropDownContextMenuProperty) as ContextMenu;
}
set
{
this.SetValue(DropDownContextMenuProperty, value);
}
}
protected override void OnClick()
{
if (DropDownContextMenu == null) return;
DropDownContextMenu.PlacementTarget = this;
DropDownContextMenu.Placement = PlacementMode.Bottom;
DropDownContextMenu.IsOpen = !DropDownContextMenu.IsOpen;
}
public static readonly DependencyProperty DropDownContextMenuProperty = DependencyProperty.Register("DropDownContextMenu", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment