첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ ToggleButton 클래스를 사용해 드롭 다운 버튼/분리 버튼을 만드는 방법을 보여준다.

TestProject.zip
0.03MB

▶ DropDownButtonStyle.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"
    xmlns:local="clr-namespace:TestProject">
    <SolidColorBrush x:Key="GlyphSolidColorBrushKey"
        Color="#444" />
    <SolidColorBrush x:Key="SelectedBackgroundSolidColorBrushKey"
        Color="#ddd" />
    <SolidColorBrush x:Key="DisabledForegroundSolidColorBrushKey"
        Color="#888" />
    <LinearGradientBrush x:Key="NormalLinearGradientBrushKey"
        StartPoint="0 0"
        EndPoint="0 1">
        <GradientStop Offset="0.0" Color="#eee" />
        <GradientStop Offset="1.0" Color="#ccc" />
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="NormalBorderLinearGradientBrushKey"
        StartPoint="0 0"
        EndPoint="0 1">
        <GradientStop Offset="0.0" Color="#ccc" />
        <GradientStop Offset="1.0" Color="#444" />
    </LinearGradientBrush>
    <Style x:Key="ButtonFocusVisualStyleKey">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle
                        Margin="3"
                        StrokeThickness="1"
                        Stroke="Black"
                        StrokeDashArray="1 2"
                        SnapsToDevicePixels="true" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <LinearGradientBrush x:Key="ButtonNormalBackgroundLinearGradientBrushKey"
        StartPoint="0.5 0"
        EndPoint="0.5 1">
        <GradientStop Offset="0"   Color="#ffffffff" />
        <GradientStop Offset="0.9" Color="#fff0f0ea" />
    </LinearGradientBrush>
    <SolidColorBrush x:Key="ButtonBorderSolidColorBrushKey"
        Color="#ff003c74" />
    <Style x:Key="LabelStyleKey" TargetType="{x:Type Label}" BasedOn="{x:Null}">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Label}">
                    <Grid>
                        <ContentPresenter
                            HorizontalAlignment="Stretch"
                            VerticalAlignment="Stretch"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            RecognizesAccessKey="True" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type local:DropDownButton}">
        <Setter Property="FocusVisualStyle"    Value="{StaticResource ButtonFocusVisualStyleKey}" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="VerticalAlignment"   Value="Center" />
        <Setter Property="MinWidth"            Value="80"     />
        <Setter Property="MinHeight"           Value="22"     />
        <Setter Property="BorderBrush"         Value="{StaticResource ButtonBorderSolidColorBrushKey}"               />
        <Setter Property="Background"          Value="{StaticResource ButtonNormalBackgroundLinearGradientBrushKey}" />
        <Setter Property="Foreground"          Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:DropDownButton}">
                    <themes:ButtonChrome Name="buttonChrome"
                        ThemeColor="NormalColor"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Fill="{TemplateBinding Background}"
                        SnapsToDevicePixels="True"
                        RenderMouseOver="{TemplateBinding IsMouseOver}"
                        RenderPressed="{TemplateBinding IsPressed}">
                        <Label Style="{StaticResource LabelStyleKey}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*"    />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Image Grid.Column="0"
                                    VerticalAlignment="Center"
                                    Margin="4 0 2 0"
                                    Width="16"
                                    Height="16"
                                    Source="{TemplateBinding Image}" />
                                <Label Grid.Column="1"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Margin="0 0 3 0"
                                    Padding="0"
                                    Target="{TemplateBinding Target}"
                                    Foreground="{TemplateBinding Foreground}"
                                    Content="{TemplateBinding Text}" />
                                <Path Grid.Column="2"
                                    Margin="3 2 2 0"
                                    Height="5"
                                    Fill="{DynamicResource GlyphSolidColorBrushKey}"
                                    Data="M 0 0 L 4 4 L 8 0 Z" />
                            </Grid>
                        </Label>
                    </themes:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="True">
                            <Setter
                                TargetName="buttonChrome"
                                Property="RenderDefaulted"
                                Value="True" />
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                            <Setter
                                TargetName="buttonChrome"
                                Property="RenderPressed"
                                Value="True" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter
                                Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ToolBarDropDownButtonStyleKey" TargetType="{x:Type local:DropDownButton}">
        <Setter Property="HorizontalAlignment"        Value="Stretch"   />
        <Setter Property="VerticalAlignment"          Value="Stretch"   />
        <Setter Property="BorderThickness"            Value="1 1 1 1"   />
        <Setter Property="BorderBrush"                Value="#00ffffff" />
        <Setter Property="Background"                 Value="#00ffffff" />
        <Setter Property="Foreground"                 Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch"   />
        <Setter Property="VerticalContentAlignment"   Value="Stretch"   />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:DropDownButton}">
                    <Border
                        BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Background="{TemplateBinding Background}"
                        SnapsToDevicePixels="True">
                        <StackPanel Orientation="Horizontal">
                            <Image
                                VerticalAlignment="Center"
                                Margin="2 2 2 2"
                                Width="16"
                                Height="16"
                                Source="{TemplateBinding Image}" />
                            <Label
                                VerticalAlignment="Center"
                                Padding="0"
                                Target="{TemplateBinding Target}"
                                Content="{TemplateBinding Text}" />
                            <Path
                                Margin="5 2 2 0"
                                Height="5"
                                Fill="{DynamicResource GlyphSolidColorBrushKey}"
                                Data="M 0 0 L 4 4 L 8 0 Z" />
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="BorderBrush" Value="#ff316ac5" />
                            <Setter Property="Background"  Value="#ffe1e6e8" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" Value="#ff316ac5" />
                            <Setter Property="Background"  Value="#ffc1d2ee" />
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="True">
                            <Setter Property="BorderBrush" Value="#ff316ac5" />
                            <Setter Property="Background"  Value="#ffc1d2ee" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="IsChecked"   Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="#ff4b4b6f" />
                            <Setter Property="Background"  Value="#ff98b5e2" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsKeyboardFocused" Value="True" />
                                <Condition Property="IsChecked"         Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="#ff4b4b6f" />
                            <Setter Property="Background"  Value="#ff98b5e2" />
                        </MultiTrigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="BorderBrush" Value="#ff4b4b6f" />
                            <Setter Property="Background"  Value="#ff98b5e2" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter
                                Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <DataTemplate x:Key="DropDownMenuIconDataTemplateKey">
        <Image Source="{Binding XPath=.}" />
    </DataTemplate>
    <ControlTemplate x:Key="DropDownMenuItemControlTemplateKey" TargetType="MenuItem">
        <Border Name="border">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"     />
                    <ColumnDefinition Width="*"                               />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut" />
                    <ColumnDefinition Width="13"                              />
                </Grid.ColumnDefinitions>
                <ContentPresenter Name="iconContentPresenter" Grid.Column="0"
                    ContentTemplate="{StaticResource DropDownMenuIconDataTemplateKey}"
                    VerticalAlignment="Center"
                    Margin="6 0 6 0"
                    Width="16"
                    Height="16"
                    ContentSource="Icon" />
                <Border Name="checkBorder" Grid.Column="0"
                    Margin="6 0 6 0"
                    Width="13"
                    Height="13"
                    BorderThickness="1"
                    BorderBrush="{StaticResource NormalBorderLinearGradientBrushKey}"
                    Background="{StaticResource NormalLinearGradientBrushKey}"
                    Visibility="Collapsed">
                    <Path Name="checkMarkPath"
                        Width="7"
                        Height="7"
                        SnapsToDevicePixels="False"
                        StrokeThickness="2"
                        Stroke="{StaticResource GlyphSolidColorBrushKey}"
                        Data="M 0 0 L 7 7 M 0 7 L 7 0"
                        Visibility="Hidden" />
                </Border>
                <ContentPresenter Grid.Column="1"
                    VerticalAlignment="Center"
                    Margin="5 2 0 2"
                    RecognizesAccessKey="True"
                    ContentSource="Header" />
                <TextBlock Grid.Column="2"
                    Margin="5 2 0 2"
                    Text="{TemplateBinding InputGestureText}" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter
                    TargetName="iconContentPresenter"
                    Property="Visibility"
                    Value="Hidden" />
            </Trigger>
            <Trigger Property="IsChecked" Value="true">
                <Setter
                    TargetName="checkMarkPath"
                    Property="Visibility"
                    Value="Visible" />
            </Trigger>
            <Trigger Property="IsCheckable" Value="true">
                <Setter
                    TargetName="checkBorder"
                    Property="Visibility"
                    Value="Visible" />
                <Setter
                    TargetName="iconContentPresenter"
                    Property="Visibility"
                    Value="Hidden" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter
                    TargetName="border"
                    Property="Background"
                    Value="{StaticResource SelectedBackgroundSolidColorBrushKey}" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter
                    Property="Foreground"
                    Value="{StaticResource DisabledForegroundSolidColorBrushKey}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>

 

▶ DropDownButton.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 드롭 다운 버튼
    /// </summary>
    public class DropDownButton : ToggleButton
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 드롭 다운 컨텍스트 메뉴 속성 - DropDownContextMenuProperty

        /// <summary>
        /// 드롭 다운 컨텍스트 메뉴 속성
        /// </summary>
        public static readonly DependencyProperty DropDownContextMenuProperty = DependencyProperty.Register
        (
            "DropDownContextMenu",
            typeof(ContextMenu),
            typeof(DropDownButton),
            new UIPropertyMetadata(null)
        );

        #endregion
        #region 이미지 속성 - ImageProperty

        /// <summary>
        /// 이미지 속성
        /// </summary>
        public static readonly DependencyProperty ImageProperty = DependencyProperty.Register
        (
            "Image",
            typeof(ImageSource),
            typeof(DropDownButton)
        );

        #endregion
        #region 텍스트 속성 - TextProperty

        /// <summary>
        /// 텍스트 속성
        /// </summary>
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register
        (
            "Text",
            typeof(string),
            typeof(DropDownButton)
        );

        #endregion
        #region 타겟 속성 - TargetProperty

        /// <summary>
        /// 타겟 속성
        /// </summary>
        public static readonly DependencyProperty TargetProperty = DependencyProperty.Register
        (
            "Target",
            typeof(UIElement),
            typeof(DropDownButton)
        );

        #endregion
        #region 드롭 다운 버튼 명령 속성 - DropDownButtonCommandProperty

        /// <summary>
        /// 드롭 다운 버튼 명령 속성
        /// </summary>
        public static readonly DependencyProperty DropDownButtonCommandProperty = DependencyProperty.Register
        (
            "DropDownButtonCommand",
            typeof(ICommand),
            typeof(DropDownButton),
            new FrameworkPropertyMetadata(null)
        );

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 드롭 다운 컨텍스트 메뉴 - DropDownContextMenu

        /// <summary>
        /// 드롭 다운 컨텍스트 메뉴
        /// </summary>
        public ContextMenu DropDownContextMenu
        {
            get
            {
                return GetValue(DropDownContextMenuProperty) as ContextMenu;
            }
            set
            {
                SetValue(DropDownContextMenuProperty, value);
            }
        }

        #endregion
        #region 이미지 - Image

        /// <summary>
        /// 이미지
        /// </summary>
        public ImageSource Image
        {
            get
            {
                return GetValue(ImageProperty) as ImageSource;
            }
            set
            {
                SetValue(ImageProperty, value);
            }
        }

        #endregion
        #region 텍스트 - Text

        /// <summary>
        /// 텍스트
        /// </summary>
        public string Text
        {
            get
            {
                return GetValue(TextProperty) as string;
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        #endregion
        #region 타겟 - Target

        /// <summary>
        /// 타겟
        /// </summary>
        public UIElement Target
        {
            get
            {
                return GetValue(TargetProperty) as UIElement;
            }
            set
            {
                SetValue(TargetProperty, value);
            }
        }

        #endregion
        #region 드롭 다운 버튼 명령 - DropDownButtonCommand

        /// <summary>
        /// 드롭 다운 버튼 명령
        /// </summary>
        public ICommand DropDownButtonCommand
        {
            get
            {
                return GetValue(DropDownButtonCommandProperty) as ICommand;
            }
            set
            {
                SetValue(DropDownButtonCommandProperty, value);
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - DropDownButton()

        /// <summary>
        /// 생성자
        /// </summary>
        public DropDownButton()
        {
            Binding binding = new Binding("DropDownContextMenu.IsOpen")
            {
                Source = this
            };

            SetBinding(IsCheckedProperty, binding);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 속성 변경시 처리하기 - OnPropertyChanged(e)

        /// <summary>
        /// 속성 변경시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);

            if(e.Property == DropDownButtonCommandProperty)
            {
                Command = DropDownButtonCommand;
            }
        }

        #endregion
        #region 클릭시 처리하기 - OnClick()

        /// <summary>
        /// 클릭시 처리하기
        /// </summary>
        protected override void OnClick()
        {
            if(DropDownContextMenu == null)
            {
                return;
            }

            if(DropDownButtonCommand != null)
            {
                DropDownButtonCommand.Execute(null);
            }

            DropDownContextMenu.PlacementTarget = this;
            DropDownContextMenu.Placement       = PlacementMode.Bottom;
            DropDownContextMenu.IsOpen          = !DropDownContextMenu.IsOpen;
        }

        #endregion
    }
}

 

▶ SplitButtonStyle.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"
    xmlns:local="clr-namespace:TestProject">
    <SolidColorBrush x:Key="GlyphSolidColorBrushKey"
        Color="#444" />
    <SolidColorBrush x:Key="SelectedBackgroundSolidColorBrushKey"
        Color="#ddd" />
    <SolidColorBrush x:Key="DisabledForegroundSolidColorBrushKey"
        Color="#888" />
    <LinearGradientBrush x:Key="NormalLinearGradientBrushKey"
        StartPoint="0 0"
        EndPoint="0 1">
        <GradientStop Offset="0.0" Color="#eee" />
        <GradientStop Offset="1.0" Color="#ccc" />
    </LinearGradientBrush>
    <LinearGradientBrush x:Key="NormalBorderLinearGradientBrushKey"
        StartPoint="0 0"
        EndPoint="0 1">
        <GradientStop Offset="0.0" Color="#ccc" />
        <GradientStop Offset="1.0" Color="#444" />
    </LinearGradientBrush>
    <Style x:Key="ButtonFocusVisualStyleKey">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle
                        Margin="3"
                        StrokeThickness="1"
                        Stroke="Black"
                        StrokeDashArray="1 2"
                        SnapsToDevicePixels="true" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <LinearGradientBrush x:Key="ButtonNormalBackgroundLinearGradientBrushKey"
        StartPoint="0.5 0"
        EndPoint="0.5 1">
        <GradientStop Offset="0"   Color="#ffffffff" />
        <GradientStop Offset="0.9" Color="#fff0f0ea" />
    </LinearGradientBrush>
    <SolidColorBrush x:Key="ButtonBorderSolidColorBrushKey"
        Color="#ff003c74" />
    <Style x:Key="MainButtonStyleKey" TargetType="{x:Type Button}" BasedOn="{x:Null}">
        <Setter Property="Margin" Value="-2 -2 0 -2" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Rectangle Name="rectangle"
                        Stroke="#00000000"
                        Fill="Transparent" />
                    <ContentPresenter
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                        RecognizesAccessKey="True" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused"   Value="True" />
                        <Trigger Property="IsDefaulted" Value="True" />
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter
                                TargetName="rectangle"
                                Property="Fill"
                                Value="#00ffffff" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                        <Setter
                            TargetName="rectangle"
                            Property="Fill"
                            Value="#e3e3dc" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False" />
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style TargetType="{x:Type local:SplitButton}">
        <Setter Property="FocusVisualStyle"    Value="{StaticResource ButtonFocusVisualStyleKey}" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="VerticalAlignment"   Value="Center" />
        <Setter Property="MinWidth"            Value="80"     />
        <Setter Property="MinHeight"           Value="22"     />
        <Setter Property="BorderBrush"         Value="{StaticResource ButtonBorderSolidColorBrushKey}"               />
        <Setter Property="Background"          Value="{StaticResource ButtonNormalBackgroundLinearGradientBrushKey}" />
        <Setter Property="Foreground"          Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:SplitButton}">
                    <themes:ButtonChrome Name="buttonChrome"
                        ThemeColor="NormalColor"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Fill="{TemplateBinding Background}"
                        SnapsToDevicePixels="True"
                        RenderMouseOver="{TemplateBinding IsMouseOver}"
                        RenderPressed="{TemplateBinding IsPressed}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"    />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <Button Name="PART_Button" Grid.Column="0"
                                Style="{DynamicResource MainButtonStyleKey}">
                                <StackPanel Orientation="Horizontal">
                                    <Image
                                        VerticalAlignment="Center"
                                        Margin="4 0 2 0"
                                        Width="16"
                                        Height="16"
                                        Source="{TemplateBinding Image}" />
                                    <Label
                                        VerticalAlignment="Center"
                                        Margin="0 0 3 0"
                                        Padding="0"
                                        Foreground="{TemplateBinding Foreground}"
                                        Target="{TemplateBinding Target}"
                                        Content="{TemplateBinding Text}" />
                                </StackPanel>
                            </Button>
                            <Border Name="lineBorder" Grid.Column="1"
                                Margin="0 -2 0 -2"
                                BorderThickness="1 0 0 0"
                                BorderBrush="#3f6c96"
                                Visibility="Visible" />
                            <Border Name="greyLineBorder" Grid.Column="2"
                                Margin="0 -2 0 -2"
                                BorderThickness="1 0 0 0"
                                BorderBrush="#b9cada"
                                Visibility="Visible" />
                            <Path Grid.Column="3"
                                Margin="3 2 2 0"
                                Height="5"
                                Fill="{DynamicResource GlyphSolidColorBrushKey}"
                                Data="M 0 0 L 4 4 L 8 0 Z" />
                        </Grid>
                    </themes:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="True">
                            <Setter
                                TargetName="buttonChrome"
                                Property="RenderDefaulted"
                                Value="True" />
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                            <Setter
                                TargetName="buttonChrome"
                                Property="RenderPressed"
                                Value="True" />
                            <Setter
                                TargetName="lineBorder"
                                Property="Visibility"
                                Value="Hidden" />
                            <Setter
                                TargetName="greyLineBorder"
                                Property="Visibility"
                                Value="Hidden" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter
                                Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                            <Setter
                                TargetName="lineBorder"
                                Property="Visibility"
                                Value="Hidden" />
                            <Setter
                                TargetName="greyLineBorder"
                                Property="Visibility"
                                Value="Hidden" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ButtonStyleKey" TargetType="{x:Type Button}">
        <Setter Property="HorizontalAlignment"        Value="Stretch"   />
        <Setter Property="VerticalAlignment"          Value="Stretch"   />
        <Setter Property="Background"                 Value="#00ffffff" />
        <Setter Property="Foreground"                 Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch"   />
        <Setter Property="VerticalContentAlignment"   Value="Stretch"   />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Name="border"
                        Background="{TemplateBinding Background}"
                        SnapsToDevicePixels="True">
                        <ContentPresenter
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            Content="{TemplateBinding Content}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter
                                TargetName="border"
                                Property="Background"
                                Value="#ff98b5e2" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="ToolBarSplitButtonStyleKey" TargetType="{x:Type local:SplitButton}">
        <Setter Property="HorizontalAlignment"        Value="Stretch"   />
        <Setter Property="VerticalAlignment"          Value="Stretch"   />
        <Setter Property="BorderThickness"            Value="1 1 1 1"   />
        <Setter Property="BorderBrush"                Value="#00ffffff" />
        <Setter Property="Background"                 Value="#00ffffff" />
        <Setter Property="Foreground"                 Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
        <Setter Property="HorizontalContentAlignment" Value="Stretch"   />
        <Setter Property="VerticalContentAlignment"   Value="Stretch"   />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:SplitButton}">
                    <Border
                        BorderThickness="{TemplateBinding BorderThickness}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        Background="{TemplateBinding Background}"
                        SnapsToDevicePixels="True">
                        <StackPanel Orientation="Horizontal">
                            <Button Name="PART_Button"
                                Style="{DynamicResource ButtonStyleKey}">
                                <StackPanel Orientation="Horizontal">
                                    <Image
                                        VerticalAlignment="Center"
                                        Margin="2 2 2 2"
                                        Width="16"
                                        Height="16"
                                        Source="{TemplateBinding Image}" />
                                    <Label
                                        VerticalAlignment="Center"
                                        Margin="0 0 3 0"
                                        Padding="0"
                                        Target="{TemplateBinding Target}"
                                        Foreground="{TemplateBinding Foreground}"
                                        Content="{TemplateBinding Text}" />
                                </StackPanel>
                            </Button>
                            <Border Name="lineBorder"
                                BorderThickness="1 0 0 0"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                Visibility="Hidden" />
                            <Path
                                Margin="2 2 2 0"
                                Height="5"
                                Fill="{DynamicResource GlyphSolidColorBrushKey}"
                                Data="M 0 0 L 4 4 L 8 0 Z" />
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="True">
                            <Setter Property="BorderBrush" Value="#ff316ac5" />
                            <Setter Property="Background"  Value="#ffe1e6e8" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" Value="#ff316ac5" />
                            <Setter Property="Background"  Value="#ffc1d2ee" />
                            <Setter
                                TargetName="lineBorder"
                                Property="Visibility"
                                Value="Visible" />
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="True">
                            <Setter Property="BorderBrush" Value="#ff316ac5" />
                            <Setter Property="Background"  Value="#ffc1d2ee" />
                            <Setter
                                TargetName="lineBorder"
                                Property="Visibility"
                                Value="Visible" />
                        </Trigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsMouseOver" Value="True" />
                                <Condition Property="IsChecked"   Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="#ff4b4b6f" />
                            <Setter Property="Background"  Value="#ff98b5e2" />
                            <Setter
                                TargetName="lineBorder"
                                Property="Visibility"
                                Value="Visible" />
                        </MultiTrigger>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsKeyboardFocused" Value="True" />
                                <Condition Property="IsChecked"         Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="BorderBrush" Value="#ff4b4b6f" />
                            <Setter Property="Background"  Value="#ff98b5e2" />
                            <Setter
                                TargetName="lineBorder"
                                Property="Visibility"
                                Value="Visible" />
                        </MultiTrigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="BorderBrush" Value="#ff4b4b6f" />
                            <Setter Property="Background"  Value="#ff98b5e2" />
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter
                                Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <DataTemplate x:Key="SplitMenuIconDataTemplateKey">
        <Image Source="{Binding XPath=.}" />
    </DataTemplate>
    <ControlTemplate x:Key="SplitMenuItemControlTemplateKey" TargetType="MenuItem">
        <Border Name="border">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"     />
                    <ColumnDefinition Width="*"                               />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut" />
                    <ColumnDefinition Width="13"                              />
                </Grid.ColumnDefinitions>
                <ContentPresenter Name="iconContentPresenter" Grid.Column="0"
                    ContentTemplate="{StaticResource SplitMenuIconDataTemplateKey}"
                    VerticalAlignment="Center"
                    Margin="6 0 6 0"
                    Width="16"
                    Height="16"
                    ContentSource="Icon" />
                <Border Name="checkBorder" Grid.Column="0"
                    Margin="6 0 6 0"
                    Width="13"
                    Height="13"
                    BorderThickness="1"
                    BorderBrush="{StaticResource NormalBorderLinearGradientBrushKey}"
                    Background="{StaticResource NormalLinearGradientBrushKey}"
                    Visibility="Collapsed">
                    <Path Name="checkMarkPath"
                        Width="7"
                        Height="7"
                        StrokeThickness="2"
                        SnapsToDevicePixels="False"
                        Stroke="{StaticResource GlyphSolidColorBrushKey}"
                        Data="M 0 0 L 7 7 M 0 7 L 7 0"
                        Visibility="Hidden" />
                </Border>
                <ContentPresenter Grid.Column="1"
                    VerticalAlignment="Center"
                    Margin="5 2 0 2"
                    RecognizesAccessKey="True"
                    ContentSource="Header" />
                <TextBlock Grid.Column="2"
                    Margin="5 2 0 2"
                    Text="{TemplateBinding InputGestureText}" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter
                    TargetName="iconContentPresenter"
                    Property="Visibility"
                    Value="Hidden" />
            </Trigger>
            <Trigger Property="IsChecked" Value="true">
                <Setter
                    TargetName="checkMarkPath"
                    Property="Visibility"
                    Value="Visible" />
            </Trigger>
            <Trigger Property="IsCheckable" Value="true">
                <Setter
                    TargetName="checkBorder"
                    Property="Visibility"
                    Value="Visible" />
                <Setter
                    TargetName="iconContentPresenter"
                    Property="Visibility"
                    Value="Hidden" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter
                    TargetName="border"
                    Property="Background"
                    Value="{StaticResource SelectedBackgroundSolidColorBrushKey}" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter
                    Property="Foreground"
                    Value="{StaticResource DisabledForegroundSolidColorBrushKey}" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>

 

▶ SplitButton.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 분리 버튼
    /// </summary>
    [TemplatePart(Name = "PART_Button", Type = typeof(ButtonBase))]
    public class SplitButton : ToggleButton
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 드롭 다운 컨텍스트 메뉴 속성 - DropDownContextMenuProperty

        /// <summary>
        /// 드롭 다운 컨텍스트 메뉴 속성
        /// </summary>
        public static readonly DependencyProperty DropDownContextMenuProperty = DependencyProperty.Register
        (
            "DropDownContextMenu",
            typeof(ContextMenu),
            typeof(SplitButton),
            new UIPropertyMetadata(null)
        );

        #endregion
        #region 이미지 속성 - ImageProperty

        /// <summary>
        /// 이미지 속성
        /// </summary>
        public static readonly DependencyProperty ImageProperty = DependencyProperty.Register
        (
            "Image",
            typeof(ImageSource),
            typeof(SplitButton)
        );

        #endregion
        #region 텍스트 속성 - TextProperty

        /// <summary>
        /// 텍스트 속성
        /// </summary>
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register
        (
            "Text",
            typeof(string),
            typeof(SplitButton)
        );

        #endregion
        #region 타겟 속성 - TargetProperty

        /// <summary>
        /// 타겟 속성
        /// </summary>
        public static readonly DependencyProperty TargetProperty = DependencyProperty.Register
        (
            "Target",
            typeof(UIElement),
            typeof(SplitButton)
        );

        #endregion
        #region 메인 버튼 명령 속성 - MainButtonCommandProperty

        /// <summary>
        /// 메인 버튼 명령 속성
        /// </summary>
        public static readonly DependencyProperty MainButtonCommandProperty = DependencyProperty.Register
        (
            "MainButtonCommand",
            typeof(ICommand),
            typeof(SplitButton),
            new FrameworkPropertyMetadata(null)
        );

        #endregion
        #region 드롭 다운 버튼 명령 속성 - DropDownButtonCommandProperty

        /// <summary>
        /// 드롭 다운 버튼 명령 속성
        /// </summary>
        public static readonly DependencyProperty DropDownButtonCommandProperty = DependencyProperty.Register
        (
            "DropDownButtonCommand",
            typeof(ICommand),
            typeof(SplitButton),
            new FrameworkPropertyMetadata(null)
        );

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 드롭 다운 컨텍스트 메뉴 - DropDownContextMenu

        /// <summary>
        /// 드롭 다운 컨텍스트 메뉴
        /// </summary>
        public ContextMenu DropDownContextMenu
        {
            get
            {
                return GetValue(DropDownContextMenuProperty) as ContextMenu;
            }
            set
            {
                SetValue(DropDownContextMenuProperty, value);
            }
        }

        #endregion
        #region 이미지 - Image

        /// <summary>
        /// 이미지
        /// </summary>
        public ImageSource Image
        {
            get
            {
                return GetValue(ImageProperty) as ImageSource;
            }
            set
            {
                SetValue(ImageProperty, value);
            }
        }

        #endregion
        #region 텍스트 - Text

        /// <summary>
        /// 텍스트
        /// </summary>
        public string Text
        {
            get
            {
                return GetValue(TextProperty) as string;
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        #endregion
        #region 타겟 - Target

        /// <summary>
        /// 타겟
        /// </summary>
        public UIElement Target
        {
            get
            {
                return GetValue(TargetProperty) as UIElement;
            }
            set
            {
                SetValue(TargetProperty, value);
            }
        }

        #endregion
        #region 메인 버튼 명령 - MainButtonCommand

        /// <summary>
        /// 메인 버튼 명령
        /// </summary>
        public ICommand MainButtonCommand
        {
            get
            {
                return GetValue(MainButtonCommandProperty) as ICommand;
            }
            set
            {
                SetValue(MainButtonCommandProperty, value);
            }
        }

        #endregion
        #region 드롭 다운 버튼 명령 - DropDownButtonCommand

        /// <summary>
        /// 드롭 다운 버튼 명령
        /// </summary>
        public ICommand DropDownButtonCommand
        {
            get
            {
                return GetValue(DropDownButtonCommandProperty) as ICommand;
            }
            set
            {
                SetValue(DropDownButtonCommandProperty, value);
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - SplitButton()

        /// <summary>
        /// 생성자
        /// </summary>
        public SplitButton()
        {
            Binding binding = new Binding("DropDownContextMenu.IsOpen")
            {
                Source = this
            };

            SetBinding(IsCheckedProperty, binding);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 템플리트 적용시 처리하기 - OnApplyTemplate()

        /// <summary>
        /// 템플리트 적용시 처리하기
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            SetMainButtonCommand();
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 속성 변경시 처리하기 - OnPropertyChanged(e)

        /// <summary>
        /// 속성 변경시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);

            if(e.Property == MainButtonCommandProperty)
            {
                SetMainButtonCommand();
            }

            if(e.Property == DropDownButtonCommandProperty)
            {
                Command = DropDownButtonCommand;
            }
        }

        #endregion
        #region 클릭시 처리하기 - OnClick()

        /// <summary>
        /// 클릭시 처리하기
        /// </summary>
        protected override void OnClick()
        {
            if(DropDownContextMenu == null)
            {
                return;
            }

            if(DropDownButtonCommand != null)
            {
                DropDownButtonCommand.Execute(null);
            }

            DropDownContextMenu.PlacementTarget = this;
            DropDownContextMenu.Placement       = PlacementMode.Bottom;

            DropDownContextMenu.IsOpen = !DropDownContextMenu.IsOpen;
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 메인 버튼 명령 설정하기 - SetMainButtonCommand()

        /// <summary>
        /// 메인 버튼 명령 설정하기
        /// </summary>
        private void SetMainButtonCommand()
        {
            if(Template != null)
            {
                ButtonBase button = Template.FindName("PART_Button", this) as ButtonBase;

                if(button != null)
                {
                    button.Command = MainButtonCommand;
                }
            }
        }

        #endregion
    }
}

 

▶ MenuInfo.cs

namespace TestProject
{
    /// <summary>
    /// 메뉴 정보
    /// </summary>
    public class MenuInfo
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 헤더 - Header

        /// <summary>
        /// 헤더
        /// </summary>
        public string Header { get; set; }

        #endregion
        #region 반환 데이터 - ReturnData

        /// <summary>
        /// 반환 데이터
        /// </summary>
        public string ReturnData { get; set; }

        #endregion
        #region 아이콘 URI - IconURI

        /// <summary>
        /// 아이콘 URI
        /// </summary>
        public string IconURI { get; set; }

        #endregion
    }
}

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="ToggleButton 클래스 : 드롭 다운 버튼/분리 버튼 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.CommandBindings>
        <CommandBinding
            Command="{x:Static local:MainWindow.SplitButtonMainCommand1}"
            Executed="SplitButtonMainExecuted1" />
        <CommandBinding
            Command="{x:Static local:MainWindow.SplitButtonMainCommand2}"
            Executed="SplitButtonMainExecuted2" />
        <CommandBinding
            Command="{x:Static local:MainWindow.SplitButtonMainCommand3}"
            Executed="SplitButtonMainExecuted3" />
    </Window.CommandBindings>
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="CONTROL/DropDownButtonStyle.xaml" />
                <ResourceDictionary Source="CONTROL/SplitButtonStyle.xaml"    />
            </ResourceDictionary.MergedDictionaries>
            <XmlDataProvider x:Key="XmlDataProviderKey">
                <x:XData>
                    <commands xmlns="">
                        <comand
                            Icon="pack://application:,,,/TestProject;component/ICON/email.ico"
                            Description="E메일 발송"
                            ReturnData="E메일" />
                        <comand
                            Icon="pack://application:,,,/TestProject;component/ICON/fax.ico"
                            Description="팩스 발송"
                            ReturnData="팩스" />
                    </commands>
                </x:XData>
            </XmlDataProvider>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"    />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*"    />
        </Grid.RowDefinitions>
        <ToolBar Grid.Row="0">
            <local:DropDownButton
                Style="{StaticResource ToolBarDropDownButtonStyleKey}"
                Image="pack://application:,,,/TestProject;component/ICON/select.ico"
                Text="드롭 다운 버튼 1">
                <local:DropDownButton.DropDownContextMenu>
                    <ContextMenu
                        ItemsSource="{Binding Source={StaticResource XmlDataProviderKey}, XPath=commands/comand}"
                        MenuItem.Click="ContextMenuItem1_Click">
                        <ContextMenu.ItemContainerStyle>
                            <Style>
                                <Setter Property="MenuItem.Template" Value="{StaticResource DropDownMenuItemControlTemplateKey}" />
                                <Setter Property="MenuItem.Header"   Value="{Binding XPath=@Description}" />
                                <Setter Property="MenuItem.Icon"     Value="{Binding XPath=@Icon}"        />
                                <Setter Property="MenuItem.Tag"      Value="{Binding XPath=@ReturnData}"  />
                            </Style>
                        </ContextMenu.ItemContainerStyle>
                    </ContextMenu>
                </local:DropDownButton.DropDownContextMenu>
            </local:DropDownButton>
            <Separator />
            <local:SplitButton
                Style="{StaticResource ToolBarSplitButtonStyleKey}"
                Image="pack://application:,,,/TestProject;component/ICON/select.ico"
                Text="분리 버튼 1"
                MainButtonCommand="{x:Static local:MainWindow.SplitButtonMainCommand1}">
                <local:SplitButton.DropDownContextMenu>
                    <ContextMenu
                        ItemsSource="{Binding Source={StaticResource XmlDataProviderKey}, XPath=commands/comand}"
                        MenuItem.Click="ContextMenuItem1_Click">
                        <ContextMenu.ItemContainerStyle>
                            <Style>
                                <Setter Property="MenuItem.Template" Value="{StaticResource SplitMenuItemControlTemplateKey}" />
                                <Setter Property="MenuItem.Header"   Value="{Binding XPath=@Description}" />
                                <Setter Property="MenuItem.Icon"     Value="{Binding XPath=@Icon}"        />
                                <Setter Property="MenuItem.Tag"      Value="{Binding XPath=@ReturnData}"  />
                            </Style>
                        </ContextMenu.ItemContainerStyle>
                    </ContextMenu>
                </local:SplitButton.DropDownContextMenu>
            </local:SplitButton>
            <Separator />
            <local:DropDownButton x:Name="dropDownButton2"
                Style="{StaticResource ToolBarDropDownButtonStyleKey}"
                Image="pack://application:,,,/TestProject;component/ICON/select.ico"
                Text="드롭 다운 버튼 2"
                Target="{Binding ElementName=dropDownButton2}"
                DropDownContextMenu="{Binding Path=ContextMenu1}" />
            <Separator />
            <local:SplitButton x:Name="splitButton2"
                Style="{StaticResource ToolBarSplitButtonStyleKey}"
                Image="pack://application:,,,/TestProject;component/ICON/select.ico"
                Text="분리 버튼 2"
                Target="{Binding ElementName=splitButton2}"
                MainButtonCommand="{x:Static local:MainWindow.SplitButtonMainCommand2}">
                <local:SplitButton.DropDownContextMenu>
                    <ContextMenu MenuItem.Click="ContextMenuItem2_Click">
                        <MenuItem Header="Send Email"
                            Tag="Email">
                            <MenuItem.Icon>
                                <Image
                                    Width="16"
                                    Height="16"
                                    Source="pack://application:,,,/TestProject;component/ICON/email.ico" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <MenuItem Header="Send Fax"
                            Tag="Fax">
                            <MenuItem.Icon>
                                <Image
                                    Width="16"
                                    Height="16"
                                    Source="pack://application:,,,/TestProject;component/ICON/fax.ico" />
                            </MenuItem.Icon>
                        </MenuItem>
                        <Separator />
                        <MenuItem Header="Send Print"
                            Tag="Print">
                            <MenuItem.Icon>
                            <Image
                                Width="16"
                                Height="16"
                                Source="pack://application:,,,/TestProject;component/ICON/print1.ico" />
                            </MenuItem.Icon>
                            <MenuItem Header="Print 1" Tag="Print 1">
                                <MenuItem.Icon>
                                    <Image
                                        Width="16"
                                        Height="16"
                                        Source="pack://application:,,,/TestProject;component/ICON/print2.ico" />
                                </MenuItem.Icon>
                            </MenuItem>
                        </MenuItem>
                    </ContextMenu>
                </local:SplitButton.DropDownContextMenu>
            </local:SplitButton>
        </ToolBar>
        <StackPanel Grid.Row="2">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"    />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="10"   />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*"    />
                </Grid.ColumnDefinitions>
                <local:DropDownButton Grid.Column="1"
                    Height="24"
                    Image="pack://application:,,,/TestProject;component/ICON/select.ico"
                    Text="드롭 다운 버튼 3"
                    DropDownContextMenu="{Binding Path=ContextMenu2}" />
                <local:SplitButton Grid.Column="3"
                    Height="24"
                    Image="pack://application:,,,/TestProject;component/ICON/select.ico"
                    Text="분리 버튼 3"
                    DropDownContextMenu="{Binding Path=ContextMenu3}"
                    MainButtonCommand="{x:Static local:MainWindow.SplitButtonMainCommand3}" />
            </Grid>
        </StackPanel>
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Xml;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Command
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 분리 버튼 메인 명령 1 - SplitButtonMainCommand1

        /// <summary>
        /// 분리 버튼 메인 명령 1
        /// </summary>
        public static RoutedUICommand SplitButtonMainCommand1 = new RoutedUICommand
        (
            "Split Button 1",
            "SplitButtonMainCommand1",
            typeof(MainWindow)
        );

        #endregion
        #region 분리 버튼 메인 명령 2 - SplitButtonMainCommand2

        /// <summary>
        /// 분리 버튼 메인 명령 2
        /// </summary>
        public static RoutedUICommand SplitButtonMainCommand2 = new RoutedUICommand
        (
            "Split Button 2",
            "SplitButtonMainCommand2",
            typeof(MainWindow)
        );

        #endregion
        #region 분리 버튼 메인 명령 3 - SplitButtonMainCommand3

        /// <summary>
        /// 분리 버튼 메인 명령 3
        /// </summary>
        public static RoutedUICommand SplitButtonMainCommand3 = new RoutedUICommand
        (
            "Split Button 3",
            "SplitButtonMainCommand3",
            typeof(MainWindow)
        );

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 컨텍스트 메뉴 1
        /// </summary>
        private ContextMenu contextMenu1;

        /// <summary>
        /// 컨텍스트 메뉴 2
        /// </summary>
        private ContextMenu contextMenu2;

        /// <summary>
        /// 컨텍스트 메뉴 3
        /// </summary>
        private ContextMenu contextMenu3;

        /// <summary>
        /// 메뉴 정보 리스트
        /// </summary>
        private readonly List<MenuInfo> menuInfoList = new List<MenuInfo>
        {
            new MenuInfo { Header="Send Email", ReturnData="Email", IconURI="pack://application:,,,/TestProject;component/ICON/Email.ico" },
            new MenuInfo { Header="Send Fax"  , ReturnData="Fax"  , IconURI="pack://application:,,,/TestProject;component/ICON/Fax.ico"   },
            new MenuInfo { Header="Send Print", ReturnData="Print", IconURI="pack://application:,,,/TestProject;component/ICON/Print.ico" }
        };

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 컨텍스트 메뉴 1 - ContextMenu1

        /// <summary>
        /// 컨텍스트 메뉴 1
        /// </summary>
        public ContextMenu ContextMenu1
        {
            get
            {
                if(this.contextMenu1 == null)
                {
                    this.contextMenu1 = new ContextMenu();

                    foreach(MenuInfo menuInfo in this.menuInfoList)
                    {
                        MenuItem menuItem = new MenuItem();

                        menuItem.Icon   = new Image
                        {
                            Width  = 16,
                            Height = 16,
                            Source = new BitmapImage(new Uri(menuInfo.IconURI))
                        };
                        menuItem.Header = menuInfo.Header;
                        menuItem.Tag    = menuInfo.ReturnData;

                        menuItem.Click += ContextMenuItem2_Click;

                        this.contextMenu1.Items.Add(menuItem);
                    }
                }

                return this.contextMenu1;
            }
        }

        #endregion
        #region 컨텍스트 메뉴 2 - ContextMenu2

        /// <summary>
        /// 컨텍스트 메뉴 2
        /// </summary>
        public ContextMenu ContextMenu2
        {
            get
            {
                if(this.contextMenu2 == null)
                {
                    this.contextMenu2 = new ContextMenu();

                    foreach(MenuInfo menuInfo in this.menuInfoList)
                    {
                        MenuItem menuItem = new MenuItem();

                        menuItem.Icon   = new Image
                        {
                            Width  = 16,
                            Height = 16,
                            Source = new BitmapImage(new Uri(menuInfo.IconURI))
                        };
                        menuItem.Header = menuInfo.Header;
                        menuItem.Tag    = menuInfo.ReturnData;

                        menuItem.Click += ContextMenuItem2_Click;

                        this.contextMenu2.Items.Add(menuItem);
                    }
                }

                return this.contextMenu2;
            }
        }

        #endregion
        #region 컨텍스트 메뉴 3 - ContextMenu3

        /// <summary>
        /// 컨텍스트 메뉴 3
        /// </summary>
        public ContextMenu ContextMenu3
        {
            get
            {
                if(this.contextMenu3 == null)
                {
                    this.contextMenu3 = new ContextMenu();

                    foreach(MenuInfo menuInfo in this.menuInfoList)
                    {
                        MenuItem menuItem = new MenuItem();

                        menuItem.Click += ContextMenuItem2_Click;

                        menuItem.Icon   = new Image
                        {
                            Width  = 16,
                            Height = 16,
                            Source = new BitmapImage(new Uri(menuInfo.IconURI))
                        };
                        menuItem.Header = menuInfo.Header;
                        menuItem.Tag    = menuInfo.ReturnData;

                        this.contextMenu3.Items.Add(menuItem);
                    }
                }

                return this.contextMenu3;
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 분리 버튼 1 메인 실행시 처리하기 - SplitButtonMainExecuted1(sender, e)

        /// <summary>
        /// 분리 버튼 1 메인 실행시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void SplitButtonMainExecuted1(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Command Click Event- Button 1.");
        }

        #endregion
        #region 분리 버튼 2 메인 실행시 처리하기 - SplitButtonMainExecuted2(sender, e)

        /// <summary>
        /// 분리 버튼 2 메인 실행시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void SplitButtonMainExecuted2(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Command Click Event - Button 2.");
        }

        #endregion
        #region 분리 버튼 3 메인 실행시 처리하기 - SplitButtonMainExecuted3(sender, e)

        /// <summary>
        /// 분리 버튼 3 메인 실행시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void SplitButtonMainExecuted3(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Command Click Event - Button 3.");
        }

        #endregion

        #region 컨텍스트 메뉴 항목 1 클릭시 처리하기 - ContextMenuItem1_Click(sender, e)

        /// <summary>
        /// 컨텍스트 메뉴 항목 1 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void ContextMenuItem1_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = e.OriginalSource as MenuItem;

            if(menuItem == null)
            {
                return;
            }

            XmlAttribute attribute = menuItem.Tag as XmlAttribute;

            if(attribute != null)
            {
                MessageBox.Show(attribute.Value);
            }
        }

        #endregion
        #region 컨테스트 메뉴 항목 2 클릭시 처리하기 - ContextMenuItem2_Click(sender, e)

        /// <summary>
        /// 컨테스트 메뉴 항목 2 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void ContextMenuItem2_Click(object sender, RoutedEventArgs e)
        {
            MenuItem menuItem = e.OriginalSource as MenuItem;

            if(menuItem != null)
            {
                MessageBox.Show(menuItem.Tag.ToString());
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요