How to make a Drop Down Button in WPF

Source: Tanguay, Edward. "How to make a Drop Down Button in WPF"

The Quince UX Pattern site has an interesting pattern called Drop Down Button which you should use when you want to conserve space but have “no primary command”. Here is how to implement it in WPF.

XAML:

<Window x:Class="TestDropdownButton.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="400" Loaded="Window_Loaded">
    <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" VerticalAlignment="Top" Margin="10">
        <TextBlock Text="The transition type is "
                     FontSize="15"/>
        <Button x:Name="Button1"
                    Content="Basic Flip"/>
        <TextBlock Text=" for this window."
                     FontSize="15"/>
        <Popup x:Name="Popup1"
                     PlacementTarget="{Binding ElementName=Button1}"
                     PopupAnimation="Fade">
            <ListBox x:Name="TransitionKind" Width="100">
                <ListBoxItem Content="Basic Flip"/>
                <ListBoxItem Content="Cube Roll"/>
                <ListBoxItem Content="Slide"/>
                <ListBoxItem Content="Fade"/>
            </ListBox>
        </Popup>
    </StackPanel>
</Window>

Code Behind:

using System.Windows;
using System.Windows.Controls;
 
namespace TestDropdownButton
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Button1.Click += new RoutedEventHandler(Button1_Click);
            TransitionKind.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(TransitionKind_SelectionChanged);
        }
 
        void TransitionKind_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            Button1.Content = ((ListBoxItem)TransitionKind.SelectedItem).Content;
            Popup1.IsOpen = false;
        }
 
        void Button1_Click(object sender, RoutedEventArgs e)
        {
            Popup1.IsOpen = true;
        }
    }
}