<span style="font-size:14px;"><UserControl x:Class="<span style="font-family: Arial,Helvetica,sans-serif;">Controls</span><span style="font-family: Arial,sans-serif;">.IPTextBox"</span> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" mc:Ignorable="d" d:DesignHeight="26" d:DesignWidth="180"> <Border BorderThickness="1" CornerRadius="1"> <Border.BorderBrush> <SolidColorBrush Opacity="0.5" Color="Black" /> </Border.BorderBrush> <Grid HorizontalAlignment="Stretch"> <Grid.ColumnDeFinitions> <ColumnDeFinition Width="*"/> <ColumnDeFinition Width="10"/> <ColumnDeFinition Width="*"/> <ColumnDeFinition Width="10"/> <ColumnDeFinition Width="*"/> <ColumnDeFinition Width="10"/> <ColumnDeFinition Width="*"/> </Grid.ColumnDeFinitions> <TextBox Grid.Column="0" Name="tbIP_1" TabIndex="1" MaxLength="3" Opacity="1" BorderThickness="0" Height="25" MinWidth="40" FontSize="15" TextAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" TextChanged="TextBox_OnTextChanged" KeyDown="TextBox_OnKeyDown" GotFocus="TextBox_OnGotFocus"> </TextBox> <sdk:Label Grid.Column="1" Background="{Binding Background,ElementName=tbIP_1}" BorderThickness="0" Height="25" Width="10" HorizontalAlignment="Center">.</sdk:Label> <TextBox Grid.Column="2" Name="tbIP_2" TabIndex="2" MaxLength="3" Opacity="1" BorderThickness="0" Height="25" MinWidth="40" FontSize="15" TextAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" TextChanged="TextBox_OnTextChanged" KeyDown="TextBox_OnKeyDown" GotFocus="TextBox_OnGotFocus"> </TextBox> <sdk:Label Grid.Column="3" Background="{Binding Background,ElementName=tbIP_1}" BorderThickness="0" Height="25" Width="10" HorizontalAlignment="Center">.</sdk:Label> <TextBox Grid.Column="4" Name="tbIP_3" TabIndex="3" MaxLength="3" Opacity="1" BorderThickness="0" Height="25" MinWidth="40" FontSize="15" TextAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" TextChanged="TextBox_OnTextChanged" KeyDown="TextBox_OnKeyDown" GotFocus="TextBox_OnGotFocus"> </TextBox> <sdk:Label Grid.Column="5" Background="{Binding Background,ElementName=tbIP_1}" BorderThickness="0" Height="25" Width="10" HorizontalAlignment="Center">.</sdk:Label> <TextBox Grid.Column="6" Name="tbIP_4" TabIndex="4" MaxLength="3" Opacity="1" BorderThickness="0" Height="25" MinWidth="40" FontSize="15" TextAlignment="Center" VerticalAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" TextChanged="TextBox_OnTextChanged" KeyDown="TextBox_OnKeyDown" GotFocus="TextBox_OnGotFocus"> </TextBox> </Grid> </Border> </UserControl></span>
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace Controls { public partial class IPTextBox : UserControl { public event EventHandler OnValueChanged; #region 自定义依赖项属性 /// <summary> /// IP地址 /// </summary> public string Text { get { return GetValue(TextProperty).ToString(); } set { SetValue(TextProperty,value); } } public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",typeof(string),typeof(IPTextBox),new PropertyMetadata(new PropertyChangedCallback(OnTextPropertyChanged))); private static void OnTextPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) { IPTextBox tb = d as IPTextBox; tb.SetIP(tb.GetValue(TextProperty).ToString()); } /// <summary> /// IP地址模式(normal:普通IP; Mask:子网掩码) /// </summary> public IPAddressMode IPMode { get { return (IPAddressMode)GetValue(IPModelProperty); } set { SetValue(IPModelProperty,value); } } public static readonly DependencyProperty IPModelProperty = DependencyProperty.Register("IPMode",typeof(IPAddressMode),new PropertyMetadata(IPAddressMode.normal)); #endregion public IPTextBox() { InitializeComponent(); } private bool isBack = false; private string GetIP() { return string.Format("{0}.{1}.{2}.{3}",tbIP_1.Text,tbIP_2.Text,tbIP_3.Text,tbIP_4.Text); } private void SetIP(string ip) { var split = ip.Split('.'); if (split.Length >= 4 && !string.IsNullOrEmpty(split[3])) tbIP_4.Text = split[3]; if (split.Length >= 3 && !string.IsNullOrEmpty(split[2])) tbIP_3.Text = split[2]; if (split.Length >= 2 && !string.IsNullOrEmpty(split[1])) tbIP_2.Text = split[1]; if (split.Length >= 1 && !string.IsNullOrEmpty(split[0])) tbIP_1.Text = split[0]; } //处理Ctrl+V private void OnPreviewKeyDown(object sender,KeyEventArgs e) { if ((e.Key == Key.V) && Keyboard.Modifiers == ModifierKeys.Control) { string clipboardString = string.Empty; if (Clipboard.ContainsText()) { clipboardString = Clipboard.GetText(); if (clipboardString.IsIPv4()) { this.Text = clipboardString; e.Handled = true; } } } } //键盘按键按下 private void TextBox_OnKeyDown(object sender,KeyEventArgs e) { if (e.Key != Key.Tab) { TextBox tb = sender as TextBox; if ((tb.Name == "tbIP_1" || tb.Name == "tbIP_4")) { //普通IP首位末位不能为0 if (IPMode == IPAddressMode.normal && (e.Key == Key.D0 || e.Key == Key.NumPad0)) { e.Handled = true; return; } } if (e.Key == Key.Decimal)//小数点键 { e.Handled = true; if (tb.Text.Length != 0) { NextGetFocus(tb.Name); } return; } else if (e.Key == Key.Back) { e.Handled = true; isBack = true; if (tb.Text.Length == 0) { PrevGetFocus(tb.Name); } return; } else if (e.Key < Key.D0 || e.Key > Key.D9 && e.Key < Key.NumPad0 || e.Key > Key.NumPad9) { e.Handled = true; return; } //最多输入三位数字 if (tb.Text.Length == 3 && tb.SelectedText.Length == 0) { e.Handled = true; return; } } } //获取到焦点 private void TextBox_OnGotFocus(object sender,RoutedEventArgs e) { TextBox tb = sender as TextBox; if (isBack)//回退事件不选中当前文本 { if (tb.Text.Length != 0) { tb.SelectionStart = tb.Text.Length; } return; } if (tb.Text.Length != 0) { tb.SelectAll(); } } //文本框文本改变 private void TextBox_OnTextChanged(object sender,TextChangedEventArgs e) { TextBox tb = sender as TextBox; string ip = this.GetIP(); if (ip == "0.0.0.0" && IPMode != IPAddressMode.Gateway) { tb.Text = ""; return; } //删除前面的0 if (tb.Text.Length == 2 && tb.Text.StartsWith("0")) { tb.Text = tb.Text.Remove(0,1); } //验证合法,大于255删除最后一位 if (tb.Text.Length == 3) { if (int.Parse(tb.Text) > 255) { tb.Text = tb.Text.Remove(2,1); tb.SelectionStart = 2; } else { NextGetFocus(tb.Name); } } this.SetValue(TextProperty,ip); if (OnValueChanged != null) { OnValueChanged(null,null); } } private void PrevGetFocus(string tbName) { switch (tbName) { case "tbIP_4": tbIP_3.Focus(); tbIP_3.SelectAll(); break; case "tbIP_3": tbIP_2.Focus(); tbIP_2.SelectAll(); break; case "tbIP_2": tbIP_1.Focus(); tbIP_1.SelectAll(); break; } } private void NextGetFocus(string tbName) { switch (tbName) { case "tbIP_1": tbIP_2.Focus(); tbIP_2.SelectAll(); break; case "tbIP_2": tbIP_3.Focus(); tbIP_3.SelectAll(); break; case "tbIP_3": tbIP_4.Focus(); tbIP_4.SelectAll(); break; } } public void Clear() { this.tbIP_1.Text = ""; this.tbIP_2.Text = ""; this.tbIP_3.Text = ""; this.tbIP_4.Text = ""; } } public enum IPAddressMode { /// <summary> /// 普通IP地址 /// </summary> normal,/// <summary> /// 子网掩码 /// </summary> Mask,/// <summary> /// 网关 /// </summary> Gateway } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。