123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- 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;
- namespace PDF_Master.CustomControl
- {
- /// <summary>
- /// NumericUpDown.xaml 的交互逻辑
- /// </summary>
- public partial class NumericUpDown : UserControl
- {
- public string Text
- {
- get { return (string)GetValue(TextProperty); }
- set { SetValue(TextProperty, value); }
- }
- public static readonly DependencyProperty TextProperty =
- DependencyProperty.Register("Text", typeof(string), typeof(NumericUpDown), new PropertyMetadata("1"));
- /// <summary>
- /// 当前值
- /// </summary>
- public double Value
- {
- get { return (double)GetValue(ValueProperty); }
- set
- {
- SetValue(ValueProperty, value);
- if (value > Maximum)
- {
- SetValue(ValueProperty, Maximum);
- }
- else if (value < Minimum)
- {
- SetValue(ValueProperty, Minimum);
- }
- else
- {
- SetValue(ValueProperty, value);
- TextBox_Num.Text = Value.ToString();
- }
- }
- }
- public static readonly DependencyProperty ValueProperty =
- DependencyProperty.Register("Value", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0, SelectedItemSourcePropertyChanged));
- private static void SelectedItemSourcePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- var control = d as NumericUpDown;
- var value = (double)e.NewValue;
- if (control != null)
- {
- control.TextBox_Num.Text = Convert.ToString(value);
- }
- }
- /// <summary>
- /// 最大值
- /// </summary>
- public double Maximum
- {
- get { return (double)GetValue(MaximumProperty); }
- set { SetValue(MaximumProperty, value); }
- }
- public static readonly DependencyProperty MaximumProperty =
- DependencyProperty.Register("Maximum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(9999.0));
- /// <summary>
- /// 最小值
- /// </summary>
- public double Minimum
- {
- get { return (double)GetValue(MinimumProperty); }
- set
- {
- SetValue(MinimumProperty, value);
- }
- }
- public static readonly DependencyProperty MinimumProperty =
- DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(0.0));
- /// <summary>
- /// 增长步长
- /// </summary>
- public double TickFrequency
- {
- get { return (double)GetValue(TickFrequencyProperty); }
- set { SetValue(TickFrequencyProperty, value); }
- }
- public static readonly DependencyProperty TickFrequencyProperty =
- DependencyProperty.Register("TickFrequency", typeof(double), typeof(NumericUpDown), new PropertyMetadata(1.0));
- public Visibility ShowBtn
- {
- get { return (Visibility)GetValue(ShowBtnProperty); }
- set { SetValue(ShowBtnProperty, value); }
- }
- public static readonly DependencyProperty ShowBtnProperty =
- DependencyProperty.Register("ShowBtn", typeof(Visibility), typeof(NumericUpDown), new PropertyMetadata(Visibility.Visible));
- public NumericUpDown()
- {
- InitializeComponent();
- }
- private void Button_Add_Click(object sender, RoutedEventArgs e)
- {
- int num = 0;
- double targetvalue = 0;
- bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
- if (result)
- {
- targetvalue = num + TickFrequency;
- }
- else
- {
- targetvalue = Maximum;
- }
- if (targetvalue > Maximum)
- {
- targetvalue = Maximum;
- }
- this.TextBox_Num.Text = targetvalue.ToString();
- Text = this.TextBox_Num.Text;
- Value = targetvalue;
- }
- private void Button_Sub_Click(object sender, RoutedEventArgs e)
- {
- int num = 0;
- double targetvalue = 0;
- bool result = int.TryParse(this.TextBox_Num.Text.Trim(), out num);
- if (result)
- {
- targetvalue = num - TickFrequency;
- }
- else
- {
- targetvalue = Minimum;
- }
- if (targetvalue < Minimum)
- {
- targetvalue = Minimum;
- }
- this.TextBox_Num.Text = targetvalue.ToString();
- Text = this.TextBox_Num.Text;
- Value = targetvalue;
- }
- private void CountTextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
- {
- e.Handled = new Regex("[^0-9]+").IsMatch(e.Text);
- }
- private void TextBox_Num_LostFocus(object sender, RoutedEventArgs e)
- {
- //校验输入的过程 放在失去焦点中,而不是放在放在文本变化事件中
- //如果放在文本变化事件中 会影响用户正常的输入,体验比较别扭
- CheckInput();
- }
- /// <summary>
- /// 校验输入是否合法
- /// </summary>
- private void CheckInput()
- {
- //控件未显示前不进行逻辑处理 避免初始化前卡顿
- if (!this.IsLoaded)
- return;
- //int.TryParse("30.0000610351563"),字符串有小数点,会转换失败
- double num = 0;
- if (/*this.TextBox_Num.Text == "" ||*/ !double.TryParse(this.TextBox_Num.Text, out num))
- {
- this.TextBox_Num.Text = Minimum.ToString();
- }
- if (double.Parse(this.TextBox_Num.Text) < Minimum)
- {
- this.TextBox_Num.Text = Minimum.ToString();
- }
- Text = this.TextBox_Num.Text;
- Value = (int)double.Parse(this.TextBox_Num.Text);
- }
- private void TextBox_Num_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- CheckInput();
- }
- }
- private void UserControl_Loaded(object sender, RoutedEventArgs e)
- {
- //修复控件初始化显示固定值的问题
- if (Value >= Minimum)
- {
- Text = Value.ToString();
- }
- }
- }
- }
|