Last active
June 12, 2017 12:27
-
-
Save shelaf/74071df2bf96a51275b9befd7c6b0647 to your computer and use it in GitHub Desktop.
WPFによるドロップダウンボタン実装試作
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Controls.Primitives; | |
using System.Windows.Data; | |
namespace WpfApplication1 | |
{ | |
/// <summary> | |
/// ドロップ ダウン メニューを表示する為のボタン コントロール クラスです。 | |
/// </summary> | |
public class DropDownButton : ToggleButton | |
{ | |
/// <summary> | |
/// インスタンスを初期化します。 | |
/// </summary> | |
public DropDownButton() | |
{ | |
Binding binding = new Binding("Menu.IsOpen") { Source = this }; | |
SetBinding(IsCheckedProperty, binding); | |
DataContextChanged += (sender, args) => | |
{ | |
if (Menu != null) | |
{ | |
Menu.DataContext = DataContext; | |
} | |
}; | |
} | |
/// <summary> | |
/// ドロップ ダウンとして表示するコンテキスト メニューを取得または設定します。 | |
/// </summary> | |
public ContextMenu Menu | |
{ | |
get | |
{ | |
return GetValue(MenuProperty) as ContextMenu; | |
} | |
set | |
{ | |
SetValue(MenuProperty, value); | |
} | |
} | |
/// <summary> | |
/// ドロップ ダウンとして表示するメニューを表す依存プロパティです。 | |
/// </summary> | |
public static readonly DependencyProperty MenuProperty = DependencyProperty.Register("Menu", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null, OnMenuChanged)); | |
private static void OnMenuChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
DropDownButton dropDownButton = d as DropDownButton; | |
ContextMenu contextMenu = e.NewValue as ContextMenu; | |
if (contextMenu != null) | |
{ | |
contextMenu.DataContext = dropDownButton.DataContext; | |
} | |
} | |
/// <summary> | |
/// コントロールがクリックされた時のイベントです。 | |
/// </summary> | |
protected override void OnClick() | |
{ | |
Debug.Print("event!!!"); | |
if (Menu != null) | |
{ | |
Menu.PlacementTarget = this; | |
Menu.Placement = PlacementMode.Bottom; | |
Menu.IsOpen = true; | |
} | |
} | |
} | |
} | |
<Window | |
x:Class="WpfApplication1.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:local="clr-namespace:WpfApplication1" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
Title="MainWindow" | |
Width="525" | |
Height="350" | |
mc:Ignorable="d"> | |
<Window.Resources> | |
<ResourceDictionary Source="Dictionary1.xaml" /> | |
</Window.Resources> | |
<Grid> | |
<DataGrid | |
x:Name="dataGrid" | |
AutoGenerateColumns="False" | |
CanUserAddRows="False" | |
CanUserDeleteRows="False" | |
DataGridCell.Selected="DataGrid_GotFocus" | |
IsReadOnly="False" | |
SelectionMode="Single" | |
SelectionUnit="Cell"> | |
<DataGrid.Columns> | |
<DataGridTemplateColumn Width="*" Header="画面"> | |
<DataGridTemplateColumn.CellTemplate> | |
<DataTemplate> | |
<TextBlock Text="{Binding ScreenName, Mode=OneWay}" /> | |
</DataTemplate> | |
</DataGridTemplateColumn.CellTemplate> | |
<DataGridTemplateColumn.CellEditingTemplate> | |
<DataTemplate> | |
<local:DropDownButton HorizontalContentAlignment="Left" Style="{StaticResource DropDownButtonStyle}"> | |
<TextBlock Text="{Binding ScreenName, Mode=OneWay}" /> | |
<local:DropDownButton.Menu> | |
<ContextMenu> | |
<MenuItem Header="aaaa" /> | |
<MenuItem Header="aaaa"> | |
<MenuItem Header="aaaa" /> | |
<MenuItem Header="aaaa" /> | |
</MenuItem> | |
<MenuItem Header="aaaa" /> | |
</ContextMenu> | |
</local:DropDownButton.Menu> | |
</local:DropDownButton> | |
</DataTemplate> | |
</DataGridTemplateColumn.CellEditingTemplate> | |
</DataGridTemplateColumn> | |
<DataGridTemplateColumn Width="*" Header="アクション"> | |
<DataGridTemplateColumn.CellTemplate> | |
<DataTemplate> | |
<TextBlock Text="{Binding ActionName, Mode=OneWay}" /> | |
</DataTemplate> | |
</DataGridTemplateColumn.CellTemplate> | |
<DataGridTemplateColumn.CellEditingTemplate> | |
<DataTemplate> | |
<ComboBox SelectedValue="{Binding ActionName, Mode=OneWay}" /> | |
</DataTemplate> | |
</DataGridTemplateColumn.CellEditingTemplate> | |
</DataGridTemplateColumn> | |
<DataGridTemplateColumn Width="*" Header="コントロール"> | |
<DataGridTemplateColumn.CellTemplate> | |
<DataTemplate> | |
<TextBlock Text="{Binding ControlId, Mode=OneWay}" /> | |
</DataTemplate> | |
</DataGridTemplateColumn.CellTemplate> | |
<DataGridTemplateColumn.CellEditingTemplate> | |
<DataTemplate> | |
<ComboBox SelectedValue="{Binding ControlId, Mode=OneWay}" /> | |
</DataTemplate> | |
</DataGridTemplateColumn.CellEditingTemplate> | |
</DataGridTemplateColumn> | |
</DataGrid.Columns> | |
</DataGrid> | |
</Grid> | |
</Window> | |
<ResourceDictionary | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:local="clr-namespace:WpfApplication1"> | |
<Style x:Key="DropDownButtonStyle" TargetType="{x:Type local:DropDownButton}"> | |
<Setter Property="BorderThickness" Value="1" /> | |
<Setter Property="IsTabStop" Value="False" /> | |
<Setter Property="HorizontalContentAlignment" Value="Center" /> | |
<Setter Property="VerticalContentAlignment" Value="Center" /> | |
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}" /> | |
<Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" /> | |
<Setter Property="Template"> | |
<Setter.Value> | |
<ControlTemplate TargetType="{x:Type local:DropDownButton}"> | |
<ToggleButton> | |
<ToggleButton.Template> | |
<ControlTemplate TargetType="ToggleButton"> | |
<ContentPresenter /> | |
</ControlTemplate> | |
</ToggleButton.Template> | |
<Border Background="Transparent"> | |
<Grid> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition /> | |
<ColumnDefinition Width="20" /> | |
</Grid.ColumnDefinitions> | |
<ContentPresenter | |
Grid.Column="0" | |
Margin="{TemplateBinding Margin}" | |
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" | |
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" /> | |
<Path | |
x:Name="Arrow" | |
Grid.Column="1" | |
HorizontalAlignment="Center" | |
VerticalAlignment="Center" | |
Data="M 0 0 L 4 4 L 8 0 Z" | |
Fill="#FF000000" /> | |
</Grid> | |
</Border> | |
</ToggleButton> | |
</ControlTemplate> | |
</Setter.Value> | |
</Setter> | |
</Style> | |
</ResourceDictionary> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment