基本思路
PreviewTextInput
OnTextChange
OnLostFocus
PreviewKeyDown
HexInput.xaml
1 <UserControl x:Class="MeterGui_BlendView.HexInput" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 mc:Ignorable="d"> 7 <TextBox x:Name="TB" PreviewTextInput="TB_PreviewTextInput" TextChanged="TB_TextChanged" PreviewKeyDown="TB_PreviewKeyDown" LostFocus="TB_LostFocus" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/> 8 </UserControl>
HexInput.xaml.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 15 namespace MeterGui_BlendView 16 { 17 /// <summary> 18 /// HexInput.xaml 的交互逻辑 19 /// </summary> 20 public partial class HexInput : UserControl 21 { 22 public HexInput() 23 { 24 InitializeComponent(); 25 Binding binding = new Binding(); 26 binding.Source = this; 27 binding.Path = new PropertyPath("HexText"); 28 binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; 29 binding.Mode = BindingMode.TwoWay; 30 TB.SetBinding(TextBox.TextProperty, binding); 31 } 32 #region DependencyProperty 33 public static readonly DependencyProperty HexTextProperty = DependencyProperty.Register( 34 "HexText",//制定属性的名称 35 typeof(string), //制定属性的类型 36 typeof(HexInput),// 37 new FrameworkPropertyMetadata(""));//设定默认值 38 public string HexText 39 { 40 get { return (string)GetValue(HexTextProperty); } 41 set { SetValue(HexTextProperty, value); } 42 } 43 44 public static readonly DependencyProperty AutoNumberProperty = DependencyProperty.Register( 45 "AutoNumber", typeof(long), typeof(HexInput),new FrameworkPropertyMetadata((long)2)); 46 public long AutoNumber 47 { 48 get { return (long)GetValue(AutoNumberProperty); } 49 set { SetValue(AutoNumberProperty, value); } 50 } 51 #endregion 52 53 //private void TB_PreviewTextInput(object sender, TextCompositionEventArgs e) 54 //{ 55 // if(!("0123456789ABCDEF".Contains(e.Text.ToUpper()))) 56 // e.Handled=true; 57 //} 58 59 //private void TB_TextChanged(object sender, TextChangedEventArgs e) 60 //{ 61 // if (!(sender as TextBox).IsFocused) 62 // { 63 // string s = (sender as TextBox).Text; 64 // FormatHex(ref s); 65 // (sender as TextBox).Text = s; 66 // } 67 //} 68 69 private void TB_LostFocus(object sender, RoutedEventArgs e) 70 { 71 string s = TB.Text; 72 FormatHex(ref s); 73 TB.Text = s; 74 } 75 76 private void TB_PreviewKeyDown(object sender, KeyEventArgs e) 77 { 78 if (false 79 || e.Key == Key.Space 80 ) 81 e.Handled = true; 82 } 83 84 private void TB_PreviewTextInput(object sender, TextCompositionEventArgs e) 85 { 86 int p = (sender as TextBox).SelectionStart; 87 int pl = (sender as TextBox).SelectionLength; 88 string s = (sender as TextBox).Text; 89 if ("0123456789ABCDEF".Contains(e.Text.ToUpper())) 90 { 91 //s=s.Remove(p+1,pl-1) 92 if (p == s.Length) 93 { 94 s = p == 0 ? s + e.Text + "0" : s + " " + e.Text + "0"; 95 p = p == 0 ? p : p + 1; 96 } 97 else 98 { 99 //如果空格结尾会有索引越界情况 100 if (s.Substring((sender as TextBox).SelectionStart, 1) == " ") 101 { 102 p = p + 1; 103 } 104 s = s.Remove(p, 1); 105 s = s.Insert(p, e.Text); 106 } 107 } 108 (sender as TextBox).Text = s; 109 (sender as TextBox).SelectionStart = p + 1; 110 e.Handled = true; 111 } 112 113 private void TB_TextChanged(object sender, TextChangedEventArgs e) 114 { 115 int p = (sender as TextBox).SelectionStart; 116 string s = (sender as TextBox).Text; 117 FormatHex(ref s); 118 (sender as TextBox).Text = s; 119 (sender as TextBox).SelectionStart = p; 120 //HexText = TB.Text.Replace(" ", ""); 121 } 122 123 private void FormatHex(ref string s,bool b=true) 124 { 125 s = s.Replace(" ", ""); 126 //s = ((s.Length % AutoNumber) == 0) ? s : (s + "0"); 127 if (s.Length >= 128) 128 s = s.Substring(0, 128); 129 if(b) 130 { 131 for (int i = ((int)AutoNumber - s.Length % (int)AutoNumber) % (int)AutoNumber; i > 0; i--) 132 { 133 s = s + "0"; 134 } 135 } 136 int l = s.Length; 137 int n = (l-2) / 2; 138 for (int i = n; i > 0; i--) 139 { 140 s = s.Insert(2 * i, " "); 141 } 142 s = s.ToUpper(); 143 } 144 145 146 } 147 }
<vi:HexInput AutoNumber="8" HexText="{Binding Model.P_option, Mode=TwoWay, Converter={StaticResource String2HexStringConverter2}}" />