1, 使用 WPF 自定制: 用 2 个 RepeatButton,用 ViewBox 包装 “<”“>”作为 Content ,再旋转 90 度。
public class SpinButton : Grid
{
private readonly RepeatButton increaseBtn = null;
private readonly RepeatButton decreaseBtn = null;
public SpinButton()
{
// build up content
this.ColumnDefinitions.Add(new ColumnDefinition());
this.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1) }); // margin
this.ColumnDefinitions.Add(new ColumnDefinition());
increaseBtn = new RepeatButton();
increaseBtn.Content = new Viewbox() { Child = new TextBlock() { Text = "<" } };
Grid.SetColumn(increaseBtn, 0);
decreaseBtn = new RepeatButton();
decreaseBtn.Content = new Viewbox() { Child = new TextBlock() { Text = ">" } };
Grid.SetColumn(decreaseBtn, 2);
this.Children.Add(increaseBtn);
this.Children.Add(decreaseBtn);
// rotate 90 degrees
this.RenderTransformOrigin = new Point(0.5, 0.5);
this.RenderTransform = new RotateTransform(90);
// hook and re-send click event
increaseBtn.Click += new RoutedEventHandler(Increase_Click);
decreaseBtn.Click += new RoutedEventHandler(Decrease_Click);
}
public event SpinButtonClicked SpinClick;
void Increase_Click(object sender, RoutedEventArgs e)
{
if (SpinClick != null)
{
SpinClick(increaseBtn, new SpinButtonEventArgs() { Delta = 1.0 });
}
}
void Decrease_Click(object sender, RoutedEventArgs e)
{
if (SpinClick != null)
{
SpinClick(decreaseBtn, new SpinButtonEventArgs() { Delta = -1.0 });
}
}
}
public delegate void SpinButtonClicked(object sender, SpinButtonEventArgs arg);
public class SpinButtonEventArgs : RoutedEventArgs
{
public double Delta { get; set; }
}
2,包装 Windows.Forms.DomainUpDown 或者 Windows.Forms.NumericUpDown
public class SpinButton : WindowsFormsHost
{
private readonly NumericUpDown upDown = null;
public SpinButton()
{
upDown = new NumericUpDown();
upDown.Increment = 5;
this.Child = upDown;
}
}