using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Windows.Controls.Primitives;
using System.Globalization;
namespace IntegerUpDown
{
public partial class IntegerUpDown : UserControl
{
public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(nameof(Value), typeof(int), typeof(IntegerUpDown));
public int Max
{
get { return (int)GetValue(MaxProperty); }
set { SetValue(MaxProperty, value); }
}
public static readonly DependencyProperty MaxProperty =
DependencyProperty.Register(nameof(Max), typeof(int), typeof(IntegerUpDown));
public int Min
{
get { return (int)GetValue(MinProperty); }
set { SetValue(MinProperty, value); }
}
public static readonly DependencyProperty MinProperty =
DependencyProperty.Register(nameof(Min), typeof(int), typeof(IntegerUpDown));
public int SmallChange
{
get { return (int)GetValue(SmallChangeProperty); }
set { SetValue(SmallChangeProperty, value); }
}
public static readonly DependencyProperty SmallChangeProperty =
DependencyProperty.Register(nameof(SmallChange), typeof(int), typeof(IntegerUpDown));
public int LargeChange
{
get { return (int)GetValue(LargeChangeProperty); }
set { SetValue(LargeChangeProperty, value); }
}
public static readonly DependencyProperty LargeChangeProperty =
DependencyProperty.Register(nameof(LargeChange), typeof(int), typeof(IntegerUpDown));
public double IntegerFontSize
{
get { return (double)GetValue(IntegerFontSizeProperty); }
set { SetValue(IntegerFontSizeProperty, value); }
}
public static readonly DependencyProperty IntegerFontSizeProperty =
DependencyProperty.Register(nameof(IntegerFontSize), typeof(double), typeof(IntegerUpDown));
public IntegerUpDown()
{
InitializeComponent();
SmallChange = 1;
LargeChange = 1;
Width = 50;
Max = 10;
Min = 0;
Value = 0;
IntegerFontSize = 14f;
MyInitialize();
this.Loaded += IntegerUpDown_Loaded;
}
private void IntegerUpDown_Loaded(object sender, RoutedEventArgs e)
{
nTextBox.Width = Width - nScrollBar.Width;
}
private void MyInitialize()
{
nScrollBar.RenderTransform = new RotateTransform(180);
nScrollBar.RenderTransformOrigin = new Point(0.5, 0.5);
nTextBox.TextAlignment = TextAlignment.Right;
nTextBox.VerticalContentAlignment = VerticalAlignment.Center;
nStackPanel.Height = Height;
MySetBinding();
MySetEvents();
}
#region バインディング関連
private void MySetBinding()
{
var b = new Binding();
b.Source = this;
b.Path = new PropertyPath(IntegerUpDown.ValueProperty);
b.Converter = new ConverterInteger2Double();
nScrollBar.SetBinding(ScrollBar.ValueProperty, b);
b = new Binding();
b.Source = nScrollBar;
b.Path = new PropertyPath(ScrollBar.ValueProperty);
b.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
nTextBox.SetBinding(TextBox.TextProperty, b);
b = new Binding();
b.Source = this;
b.Path = new PropertyPath(MaxProperty);
b.Converter = new ConverterInteger2Double();
nScrollBar.SetBinding(ScrollBar.MaximumProperty, b);
b = new Binding();
b.Source = this;
b.Path = new PropertyPath(MinProperty);
b.Converter = new ConverterInteger2Double();
nScrollBar.SetBinding(ScrollBar.MinimumProperty, b);
b = new Binding();
b.Source = this;
b.Path = new PropertyPath(SmallChangeProperty);
b.Mode = BindingMode.OneWay;
nScrollBar.SetBinding(ScrollBar.SmallChangeProperty, b);
b = new Binding();
b.Source = this;
b.Path = new PropertyPath(LargeChangeProperty);
b.Mode = BindingMode.OneWay;
nScrollBar.SetBinding(ScrollBar.LargeChangeProperty, b);
b = new Binding();
b.Source = this;
b.Path = new PropertyPath(IntegerFontSizeProperty);
b.Mode = BindingMode.OneWay;
nTextBox.SetBinding(TextBox.FontSizeProperty, b);
}
#endregion
#region イベント関連
private void MySetEvents()
{
nTextBox.TextChanged += NTextBox_TextChanged;
nTextBox.LostFocus += NTextBox_LostFocus;
nTextBox.GotFocus += NTextBox_GotFocus;
nTextBox.MouseWheel += NTextBox_MouseWheel;
nScrollBar.MouseWheel += NScrollBar_MouseWheel;
}
private void NTextBox_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (e.Delta > 0) { nScrollBar.Value += SmallChange; }
else { nScrollBar.Value -= SmallChange; }
}
private void NScrollBar_MouseWheel(object sender, MouseWheelEventArgs e)
{
if (e.Delta > 0) { nScrollBar.Value += LargeChange; }
else { nScrollBar.Value -= LargeChange; }
}
private void NTextBox_GotFocus(object sender, RoutedEventArgs e)
{
TextBox box = (TextBox)sender;
this.Dispatcher.InvokeAsync(() =>
{
Task.Delay(10);
box.SelectAll();
});
}
private void NTextBox_LostFocus(object sender, RoutedEventArgs e)
{
TextBox box = (TextBox)sender;
int i;
if (int.TryParse(box.Text, out i) == true)
{
Value = i;
}
else
{
Value = Min;
box.Text = Min.ToString();
}
}
private void NTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox box = (TextBox)sender;
double d;
if (!double.TryParse(box.Text, out d))
{
box.Text = Regex.Replace(box.Text, "[^0-9-]", "");
}
}
#endregion
}
internal class ConverterInteger2Double : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int i = (int)value;
return (double)i;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
double d = (double)value;
return (int)d;
}
}
}