wpf DomainUpDownコントロール

wpfの、DomainUpDownコントロールを、作ってみました。

 

まずは、コードです。

 

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

namespace OkaSharp
{

 

[TemplatePart(Name = "PART_UpButton", Type = typeof(RepeatButton))]
[TemplatePart(Name = "PART_DownButton", Type = typeof(RepeatButton))]
[TemplateVisualState(Name = "Focused", GroupName = "FocusedStates")]
[TemplateVisualState(Name = "Unfocused", GroupName = "FocusedStates")]
public class DomainUpDown : Selector
{
static DomainUpDown()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DomainUpDown), new FrameworkPropertyMetadata(typeof(DomainUpDown)));
}

public DomainUpDown()
{
DefaultStyleKey = typeof(DomainUpDown);
this.IsTabStop = true;
}

 


private void UpdateStates(bool useTransitions)
{

if (IsFocused)
{
VisualStateManager.GoToState(this, "Focused", useTransitions);
}
else
{
VisualStateManager.GoToState(this, "Unfocused", useTransitions);
}

}

public override void OnApplyTemplate()
{
UpButtonElement = GetTemplateChild("PART_UpButton") as RepeatButton;
DownButtonElement = GetTemplateChild("PART_DownButton") as RepeatButton;
//TextElement = GetTemplateChild("TextBlock") as TextBlock;

UpdateStates(false);
}

private RepeatButton downButtonElement;

private RepeatButton DownButtonElement
{
get
{
return downButtonElement;
}

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

if (downButtonElement != null)
{
downButtonElement.Click +=
new RoutedEventHandler(downButtonElement_Click);
}
}
}

void downButtonElement_Click(object sender, RoutedEventArgs e)
{

if*1
SelectedIndex--;
}

private RepeatButton upButtonElement;

private RepeatButton UpButtonElement
{
get
{
return upButtonElement;
}

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

if (upButtonElement != null)
{
upButtonElement.Click +=
new RoutedEventHandler(upButtonElement_Click);
}
}
}

void upButtonElement_Click(object sender, RoutedEventArgs e)
{
if *2
SelectedIndex++;
}

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
Focus();
}


protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
UpdateStates(true);
}

protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
UpdateStates(true);
}
}

 

}

 

次に、スタイルです。

 

<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 TargetType="{x:Type local:DomainUpDown}" x:Key="{x:Type local:DomainUpDown}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:DomainUpDown">
<Grid Background="{TemplateBinding Background}">


<VisualStateManager.VisualStateGroups>

 

<VisualStateGroup Name="FocusStates">


<VisualState Name="Focused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Duration="0"
Storyboard.TargetName="FocusVisual"
Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>

<!--
Return the control to its initial state by
hiding the focus rectangle.
-->
<VisualState Name="Unfocused" />
</VisualStateGroup>

</VisualStateManager.VisualStateGroups>

<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="25" />
</Grid.ColumnDefinitions>

<Border Grid.RowSpan="2"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Background="#E0FFFFFF"
BorderBrush="Gray"
BorderThickness="1">

Content="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type local:DomainUpDown}},
Path=SelectedItem}"
Padding="0"


/>
</Border>

<Rectangle Name="FocusVisual"
Grid.RowSpan="2"
Grid.ColumnSpan="2"
Stroke="Black"
StrokeDashArray="0,1"
StrokeThickness="1"
Visibility="Collapsed" />
<RepeatButton Name="PART_UpButton"
Grid.Row="0"
Grid.Column="1"
Content="▲"
FontSize="8" />
<RepeatButton Name="PART_DownButton"
Grid.Row="1"
Grid.Column="1"
Content="▼"
FontSize="8" />

</Grid>

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

</ResourceDictionary>

 

 

 

 

 

*1:SelectedIndex > 0

*2:SelectedIndex < Items.Count - 1