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 compdfkit_tools.Common
{
///
/// NumberBoxDropDownControl.xaml 的交互逻辑
///
public partial class DropDownNumberBoxControl : UserControl
{
private string regixString = "[^0-9]+";
// The dependency property which will be accessible on the UserControl
public static readonly DependencyProperty UnitProperty =
DependencyProperty.Register("Unit", typeof(string), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
public string Unit
{
get { return (string)GetValue(UnitProperty); }
set { SetValue(UnitProperty, value); }
}
// The dependency property which will be accessible on the UserControl
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty MaxiumProperty =
DependencyProperty.Register("Maxium", typeof(int), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
public int Maxium
{
get { return (int)GetValue(MaxiumProperty); }
set { SetValue(MaxiumProperty, value); }
}
public static readonly DependencyProperty MinimumProperty =
DependencyProperty.Register("Minimum", typeof(int), typeof(DropDownNumberBoxControl), new UIPropertyMetadata());
public int Minimum
{
get { return (int)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
public event EventHandler InputEnterEvent;
public event EventHandler SetPresetEvent;
public DropDownNumberBoxControl()
{
InitializeComponent();
}
public void InitPresetNumberArray(List presetNumber)
{
List array = new List();
for (int i = 0; i < presetNumber.Count; i++)
{
array.Add(presetNumber[i].ToString());
}
ComboBox.ItemsSource = array;
ComboBox.SelectedIndex = 0;
}
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = new Regex(regixString).IsMatch(e.Text);
}
private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
InputEnterEvent?.Invoke(sender, Text);
}
if ((e.KeyStates == Keyboard.GetKeyStates(Key.LeftCtrl) || e.KeyStates == Keyboard.GetKeyStates(Key.RightCtrl)) && e.KeyStates == Keyboard.GetKeyStates(Key.V))
e.Handled = true;
else
e.Handled = false;
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (!string.IsNullOrEmpty(TextBox.Text))
{
if (int.Parse(TextBox.Text) > Maxium)
{
TextBox.Text = Maxium.ToString();
}
if (int.Parse(TextBox.Text) < Minimum)
{
TextBox.Text = Minimum.ToString();
}
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if ((sender as ComboBox).SelectedItem != null)
{
// 处理空引用异常的代码
TextBox.Text = (sender as ComboBox).SelectedItem.ToString();
SetPresetEvent?.Invoke(sender, TextBox.Text);
}
}
}
}