|
つづき
-- NumericUpDown Xaml --
<UserControl x:Class="UI.Controls.NumericUpDown"
- 省略
mc:Ignorable="d" Loaded="UserControl_Loaded">
<Grid Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="15"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="valueText" Text="{Binding ValueText.Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
BorderBrush="Transparent" TextChanged="TextBox_TextChanged" >
</TextBox>
<Grid Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Button x:Name="upButton" Content="▲" FontSize="3" BorderBrush="Transparent" Command="{Binding UpCommand}"/>
<Button x:Name="downButton" Grid.Row="1" Content="▼" FontSize="3" BorderBrush="Transparent" Command="{Binding DownCommand}"/>
</Grid>
</Grid>
</UserControl>
-- NumericUpDown Xaml.cs --
// 1. 依存プロパティの作成
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value",
typeof(object),
typeof(NumericUpDown),
new FrameworkPropertyMetadata("Value", new PropertyChangedCallback(OnValueChanged)));
public static readonly DependencyProperty MinProperty =
DependencyProperty.Register("Min",
typeof(int),
typeof(NumericUpDown),
new FrameworkPropertyMetadata("Min", new PropertyChangedCallback(OnMinChanged)));
public static readonly DependencyProperty MaxProperty =
DependencyProperty.Register("Max",
typeof(int),
typeof(NumericUpDown),
new FrameworkPropertyMetadata("Max", new PropertyChangedCallback(OnMaxChanged)));
// 2. CLI用プロパティを提供するラッパー
public object Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public int Min
{
get { return (int)GetValue(MinProperty); }
set { SetValue(MinProperty, value); }
}
public int Max
{
get { return (int)GetValue(MaxProperty); }
set { SetValue(MaxProperty, value); }
}
// 3. 依存プロパティが変更されたとき呼ばれるコールバック関数の定義
private static void OnValueChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
- 省略
}
private static void OnMinChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
- 省略
}
private static void OnMaxChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
- 省略
}
public NumericUpDown()
{
InitializeComponent();
this.DataContext = new ViewModels.NumericUpDownViewModel();
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
Value = valueText.Text;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
NumericUpDown ctrl = sender as NumericUpDown;
var vm = this.DataContext as ViewModels.NumericUpDownViewModel;
if (vm != null)
{
vm.Min = ctrl.Min;
vm.Max = ctrl.Max;
vm.ValueText.Value = ctrl.Value.ToString();
}
}
-- NumericUpDownViewModel --
public NumericUpDownViewModel()
{
// 値を上げる
UpCommand.Subscribe(_ =>
{
if (string.IsNullOrEmpty(ValueText.Value))
{
ValueText.Value = $"{Min:D02}";
return;
}
var value = int.Parse(ValueText.Value);
value += 1;
if (value > Max)
{
value = Min;
}
ValueText.Value = $"{value:D02}";
});
// 値を下げる
DownCommand.Subscribe(_ =>
{
if (string.IsNullOrEmpty(ValueText.Value))
{
ValueText.Value = $"{Max:D02}";
return;
}
var value = int.Parse(ValueText.Value);
value -= 1;
if (Min > value)
{
value = Max;
}
ValueText.Value = $"{value:D02}";
});
}
public ReactiveProperty<string> ValueText { get; } = new ReactiveProperty<string>();
public ReactiveCommand UpCommand { get; } = new ReactiveCommand();
public ReactiveCommand DownCommand { get; } = new ReactiveCommand();
public int Min { get; set; }
public int Max { get; set; }
|