Another behavior that I wrote for my project. The code is simple and there aren’t many things to configure, but it is a good starting point for a nice button popup (many other examples I’ve seen are coded as custom controls; the same can be accomplished using this behavior):
namespace Foo
{
public class ButtonPopupBehavior : Behavior<Button>
{
private readonly Popup popup_;
public static DependencyProperty PopupProperty =
DependencyProperty.Register(
"Popup",
typeof(UIElement),
typeof(ButtonPopupBehavior),
null
);
public ButtonPopupBehavior()
{
popup_ = new Popup() { StaysOpen = false };
}
public UIElement Popup
{
get { return (UIElement)GetValue(PopupProperty); }
set { SetValue(PopupProperty, value); }
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Click += OnClick;
AssociatedObject.IsVisibleChanged += OnIsVisibleChanged;
popup_.PlacementTarget = AssociatedObject;
popup_.Child = Popup;
}
protected override void OnDetaching()
{
AssociatedObject.Click -= OnClick;
AssociatedObject.IsVisibleChanged -= OnIsVisibleChanged;
base.OnDetaching();
}
private void OnClick(object sender, RoutedEventArgs e)
{
popup_.IsOpen = true;
}
private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (!(bool)e.NewValue)
popup_.IsOpen = false;
}
}
}

April 15, 2011 at 9:23 am
This looks very good, but could you please provide an example of how to use the behavior from XAML?
April 16, 2011 at 11:15 pm
It can be used something like this:
<Button> <Interactivity:Interaction.Behaviors> <Behaviors:ButtonPopupBehavior> <Behaviors:ButtonPopupBehavior.Popup> <!-- put here any control/controls you like to be shown as pop-up --> </Behaviors:ButtonPopupBehavior.Popup> </Behaviors:ButtonPopupBehavior> </Interactivity:Interaction.Behaviors> </Button>