wpfのDropDownButton

office等で、よく見る、DropDownButtonを、このブログでは、DropDownButtonと、DropDownMenuButtonと、

DDButtonの、3通りの呼び方をします。

 

それを、wpfで、作ってみました。

 

まずは、コードです。

 

using System;

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace OkaSharp
{

 

/// <summary>
/// ドロップ ダウン メニューを表示する為のボタン コントロール クラスです。
/// </summary>
///
[TemplatePart(Name = "PART_DropDownButton", Type = typeof(ToggleButton))]
public sealed class DropDownButton : Control
{

static DropDownButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DropDownButton), new FrameworkPropertyMetadata(typeof(DropDownButton)));
}

/// <summary>
/// インスタンスを初期化します。
/// </summary>
public DropDownButton()
{

}

public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register(
"Content", typeof(object), typeof(DropDownButton),
new PropertyMetadata(((Object)"")));

public object Content
{
get
{
return (object)GetValue(ContentProperty);
}

set
{


SetValue(ContentProperty, value);

 

}
}

public static readonly DependencyProperty DropDownContentProperty =
DependencyProperty.Register(
"DropDownContent", typeof(object), typeof(DropDownButton),
new PropertyMetadata(((Object)"")));

public object DropDownContent
{
get
{
return (object)GetValue(DropDownContentProperty);
}

set
{


SetValue(DropDownContentProperty, value);

 

}
}

private void UpdateStates(bool useTransitions)
{


}

public override void OnApplyTemplate()
{

DropDownButtonElement = GetTemplateChild("PART_DropDownButton") as ToggleButton;


UpdateStates(false);
}

private ToggleButton dropDownButtonElement;

private ToggleButton DropDownButtonElement
{
get
{
return dropDownButtonElement;
}

set
{
if (dropDownButtonElement != null)
{
dropDownButtonElement.Click -=
new RoutedEventHandler(dropDownButtonElement_Click);
}
dropDownButtonElement = value;

if (dropDownButtonElement != null)
{
dropDownButtonElement.Click +=
new RoutedEventHandler(dropDownButtonElement_Click);
}
}
}

void dropDownButtonElement_Click(object sender, RoutedEventArgs e)
{

}

 

/// <summary>
/// ドロップ ダウンとして表示するメニューを表す依存プロパティです。
/// </summary>
public static readonly DependencyProperty DropDownContextMenuProperty = DependencyProperty.Register("DropDownContextMenu", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null));
}

 

}

 

次に、スタイルです。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:OkaSharp"
>

 

<Style x:Key="{x:Type local:DropDownButton}" TargetType="{x:Type local:DropDownButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DropDownButton">
<Grid Background="{TemplateBinding Background}">

 


<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>


<ToggleButton Name="PART_DropDownButton"
Grid.Row="0"
Grid.Column="0">
<StackPanel Orientation="Horizontal">
<ContentPresenter Content="{TemplateBinding Content}" />

<TextBlock Text="▼" />
</StackPanel>
</ToggleButton>

 

</Grid>
<Popup AllowsTransparency="True"
Focusable="False"
HorizontalOffset="1"
IsOpen="{Binding Path=IsChecked,
ElementName=PART_DropDownButton}"
Placement="Bottom"
StaysOpen="False"
VerticalOffset="1">
<Border Background="White" BorderBrush="Gray">
<ContentPresenter Content="{TemplateBinding DropDownContent}" />

</Border>
</Popup>

</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

 

</ResourceDictionary>