最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于WPF實現(xiàn)IP輸入控件

 更新時間:2023年08月25日 15:37:14   作者:WPF開發(fā)者  
這篇文章主要介紹了如何基于WPF實現(xiàn)簡單的IP輸入控件,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,需要的小伙伴可以參考一下

WPF 實現(xiàn)IP輸入控件

  • 框架使用.NET4 至 .NET6;
  • Visual Studio 2022;

使用Grid來分為七列

  • TextBox控件PART_TextBox1位于第一列。
  • TextBox控件PART_TextBox2位于第三列。
  • TextBox控件PART_TextBox3位于第五列。
  • TextBox控件PART_TextBox4位于第七列。

實現(xiàn)代碼

1)新增 IPEditBox.cs 代碼如下:

TextBox1_TextChanged方法:當TextBox1的文本發(fā)生變化時觸發(fā)。如果TextBox1中的文本長度大于等于3,焦點將轉(zhuǎn)移到TextBox2,并調(diào)用UpdateText方法更新Text。

TextBox2_TextChanged方法:當TextBox2的文本發(fā)生變化時觸發(fā)。如果TextBox2中的文本長度大于等于3,焦點將轉(zhuǎn)移到TextBox3,并調(diào)用UpdateText方法更新Text。

TextBox3_TextChanged方法:當TextBox3的文本發(fā)生變化時觸發(fā)。如果TextBox3中的文本長度大于等于3,焦點將轉(zhuǎn)移到TextBox4,并調(diào)用UpdateText方法更新Text。

TextBox4_TextChanged方法:當TextBox4的文本發(fā)生變化時觸發(fā)。無論文本長度如何,并調(diào)用UpdateText方法更新Text。

監(jiān)聽TextBox1、2、3、4控件的PreviewKeyDown事件處理。

  • 是否按下Ctrl+V鍵組合(粘貼快捷鍵),
  • 如果是,則調(diào)用ClipboardHandle()方法,并將事件參數(shù)的Handled屬性設置為true。
  • PasteTextIPTextBox方法負責處理粘貼的文本。如果粘貼的文本為空或只包含空白字符,則清除四個TextBox控件(_textBox1、_textBox2、_textBox3、_textBox4)的內(nèi)容。否則,它通過句點('.')將文本分割,并將每個部分分配給_textboxBoxes數(shù)組中對應的TextBox控件。

監(jiān)聽TextBox1、2、3、4控件的Loaded事件處理。在TextBox加載完成時,通過CommandManager的AddPreviewExecutedHandler方法將TextBox的PreviewExecuted事件與TextBox_PreviewExecuted方法關聯(lián)

  • 如果執(zhí)行的是復制(ApplicationCommands.Copy),則將_textBox1、_textBox2、_textBox3、_textBox4四個TextBox控件中的文本合并成一個IP地址字符串,并將該字符串設置為剪貼板的文本內(nèi)容并更新Text。
  • 如果是粘貼命令則將調(diào)用ClipboardHandle()方法。
using?System.Linq;
using?System.Windows;
using?System.Windows.Controls;
using?System.Windows.Input;
namespace?WPFDevelopers.Controls
{
?????[TemplatePart(Name?=?TextBox1TemplateName,?Type?=?typeof(TextBox))]
????[TemplatePart(Name?=?TextBox2TemplateName,?Type?=?typeof(TextBox))]
????[TemplatePart(Name?=?TextBox3TemplateName,?Type?=?typeof(TextBox))]
????[TemplatePart(Name?=?TextBox4TemplateName,?Type?=?typeof(TextBox))]
????public?class?IPEditBox?:?Control
????{
????????private?const?string?TextBox1TemplateName?=?"PART_TextBox1";
????????private?const?string?TextBox2TemplateName?=?"PART_TextBox2";
????????private?const?string?TextBox3TemplateName?=?"PART_TextBox3";
????????private?const?string?TextBox4TemplateName?=?"PART_TextBox4";
????????public?string?Text
????????{
????????????get?{?return?(string)GetValue(TextProperty);?}
????????????set?{?SetValue(TextProperty,?value);?}
????????}
????????public?static?readonly?DependencyProperty?TextProperty?=
????????????DependencyProperty.Register("Text",?typeof(string),?typeof(IPEditBox),?new?PropertyMetadata(string.Empty,?OnTextChanged));
????????private?static?void?OnTextChanged(DependencyObject?d,?DependencyPropertyChangedEventArgs?e)
????????{
????????????var?ctrl?=?d?as?IPEditBox;
????????????if?(e.NewValue?is?string?text?&&?!ctrl._isChangingText)
????????????????ctrl.PasteTextIPTextBox(text);
????????}
????????private?TextBox?_textBox1,?_textBox2,?_textBox3,?_textBox4;
????????private?bool?_isChangingText?=?false;
????????public?override?void?OnApplyTemplate()
????????{
????????????base.OnApplyTemplate();
????????????_textBox1?=?GetTemplateChild(TextBox1TemplateName)?as?TextBox;
????????????if?(_textBox1?!=?null)
????????????{
????????????????_textBox1.TextChanged?-=?TextBox1_TextChanged;
????????????????_textBox1.TextChanged?+=?TextBox1_TextChanged;
????????????????_textBox1.PreviewKeyDown?-=?TextBox_PreviewKeyDown;
????????????????_textBox1.PreviewKeyDown?+=?TextBox_PreviewKeyDown;
????????????????_textBox1.Loaded?-=?TextBox_Loaded;
????????????????_textBox1.Loaded?+=?TextBox_Loaded;
????????????}
????????????_textBox2?=?GetTemplateChild(TextBox2TemplateName)?as?TextBox;
????????????if?(_textBox2?!=?null)
????????????{
????????????????_textBox2.TextChanged?-=?TextBox2_TextChanged;
????????????????_textBox2.TextChanged?+=?TextBox2_TextChanged;
????????????????_textBox2.PreviewKeyDown?-=?TextBox_PreviewKeyDown;
????????????????_textBox2.PreviewKeyDown?+=?TextBox_PreviewKeyDown;
????????????????_textBox2.Loaded?-=?TextBox_Loaded;?;
????????????????_textBox2.Loaded?+=?TextBox_Loaded;
????????????}
????????????_textBox3?=?GetTemplateChild(TextBox3TemplateName)?as?TextBox;
????????????if?(_textBox3?!=?null)
????????????{
????????????????_textBox3.TextChanged?-=?TextBox3_TextChanged;
????????????????_textBox3.TextChanged?+=?TextBox3_TextChanged;
????????????????_textBox3.PreviewKeyDown?-=?TextBox_PreviewKeyDown;
????????????????_textBox3.PreviewKeyDown?+=?TextBox_PreviewKeyDown;
????????????????_textBox3.Loaded?-=?TextBox_Loaded;
????????????????_textBox3.Loaded?+=?TextBox_Loaded;
????????????}
????????????_textBox4?=?GetTemplateChild(TextBox4TemplateName)?as?TextBox;
????????????_textBox4.TextChanged?-=?TextBox4_TextChanged;
????????????_textBox4.TextChanged?+=?TextBox4_TextChanged;
????????????_textBox4.PreviewKeyDown?-=?TextBox_PreviewKeyDown;
????????????_textBox4.PreviewKeyDown?+=?TextBox_PreviewKeyDown;
????????????_textBox4.Loaded?-=?TextBox_Loaded;
????????????_textBox4.Loaded?+=?TextBox_Loaded;
????????}
????????private?void?TextBox1_TextChanged(object?sender,?TextChangedEventArgs?e)
????????{
????????????if?(_textBox1.Text.ToString().Length?>=?3)?_textBox2.Focus();
????????????UpdateText();
????????}
????????private?void?TextBox2_TextChanged(object?sender,?TextChangedEventArgs?e)
????????{
????????????if?(_textBox2.Text.ToString().Length?>=?3)?_textBox3.Focus();
????????????UpdateText();
????????}
????????private?void?TextBox3_TextChanged(object?sender,?TextChangedEventArgs?e)
????????{
????????????if?(_textBox3.Text.ToString().Length?>=?3)?_textBox4.Focus();
????????????UpdateText();
????????}
????????private?void?TextBox4_TextChanged(object?sender,?TextChangedEventArgs?e)
????????{
????????????UpdateText();
????????}
????????void?TextBox_Loaded(object?sender,?RoutedEventArgs?e)
????????{
????????????CommandManager.AddPreviewExecutedHandler((sender?as?TextBox),?TextBox_PreviewExecuted);
????????}
????????void?TextBox_PreviewExecuted(object?sender,?ExecutedRoutedEventArgs?e)
????????{
????????????if?(e.Command?==?ApplicationCommands.Paste)
????????????{
????????????????ClipboardHandle();
????????????????UpdateText();
????????????????e.Handled?=?true;
????????????}
????????????else?if?(e.Command?==?ApplicationCommands.Copy)
????????????{
????????????????var?ip?=?$"{_textBox1.Text}.{_textBox2.Text}.{_textBox3.Text}.{_textBox4.Text}";
????????????????Clipboard.SetText(ip);
????????????????e.Handled?=?true;
????????????}
????????}
????????void?ClipboardHandle()
????????{
????????????var?data?=?Clipboard.GetDataObject();
????????????if?(data.GetDataPresent(DataFormats.Text))
????????????{
????????????????var?text?=?(string)data.GetData(DataFormats.UnicodeText);
????????????????PasteTextIPTextBox(text);
????????????}
????????}
????????void?TextBox_PreviewKeyDown(object?sender,?KeyEventArgs?e)
????????{
????????????if?(e.KeyboardDevice.Modifiers.HasFlag(ModifierKeys.Control)?&&?e.Key?==?Key.V)
????????????{
????????????????ClipboardHandle();
????????????????_isChangingText?=?false;
????????????????e.Handled?=?true;
????????????}
????????????else?if?(e.Key?==?Key.Delete?||?e.Key?==?Key.Back)
????????????{
????????????????_isChangingText?=?true;
????????????}
????????????else
????????????????_isChangingText?=?false;
????????}
????????void?PasteTextIPTextBox(string?text)
????????{
????????????_textBox1.TextChanged?-=?TextBox1_TextChanged;
????????????_textBox2.TextChanged?-=?TextBox2_TextChanged;
????????????_textBox3.TextChanged?-=?TextBox3_TextChanged;
????????????_textBox4.TextChanged?-=?TextBox4_TextChanged;
????????????if?(string.IsNullOrWhiteSpace(text))
????????????{
????????????????_textBox1.Text?=?string.Empty;
????????????????_textBox2.Text?=?string.Empty;
????????????????_textBox3.Text?=?string.Empty;
????????????????_textBox4.Text?=?string.Empty;
????????????}
????????????else
????????????{
????????????????var?strs?=?text.Split('.');
????????????????var?_textboxBoxes?=?new?TextBox[]?{?_textBox1,?_textBox2,?_textBox3,?_textBox4?};
????????????????for?(short?i?=?0;?i?<?_textboxBoxes.Length;?i++)
????????????????{
????????????????????var?str?=?i?<?strs.Length???strs[i]?:?string.Empty;
????????????????????_textboxBoxes[i].Text?=?str;
????????????????}
????????????}
????????????_textBox1.TextChanged?+=?TextBox1_TextChanged;
????????????_textBox2.TextChanged?+=?TextBox2_TextChanged;
????????????_textBox3.TextChanged?+=?TextBox3_TextChanged;
????????????_textBox4.TextChanged?+=?TextBox4_TextChanged;
????????}
????????void?UpdateText()
????????{
????????????var?segments?=?new?string[4]
????????????{
????????????????_textBox1.Text.Trim(),
????????????????_textBox2.Text.Trim(),
????????????????_textBox3.Text.Trim(),
????????????????_textBox4.Text.Trim()
????????????};
????????????var?allEmpty?=?segments.All(string.IsNullOrEmpty);
????????????if?(allEmpty)
????????????{
????????????????SetValue(TextProperty,?string.Empty);
????????????????return;
????????????}
????????????var?noEmpty?=?segments.Where(s?=>?!string.IsNullOrWhiteSpace(s));
????????????if?(noEmpty.Count()?!=?4)?return;
????????????var?ip?=?string.Join(".",?noEmpty);
????????????if?(ip?!=?Text)
????????????????SetValue(TextProperty,?ip);
????????}
????}
}

2)新增 ColorPicker.xaml 代碼如下:

?<ResourceDictionary
????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
????xmlns:controls="clr-namespace:WPFDevelopers.Controls"
????xmlns:helpers="clr-namespace:WPFDevelopers.Helpers">
????<ResourceDictionary.MergedDictionaries>
????????<ResourceDictionary?Source="Basic/ControlBasic.xaml"?/>
????</ResourceDictionary.MergedDictionaries>
????<Style
????????x:Key="WD.IPEditBox"
????????BasedOn="{StaticResource?WD.ControlBasicStyle}"
????????TargetType="{x:Type?controls:IPEditBox}">
????????<Setter?Property="BorderThickness"?Value="1"?/>
????????<Setter?Property="BorderBrush"?Value="{DynamicResource?WD.BaseSolidColorBrush}"?/>
????????<Setter?Property="Background"?Value="{DynamicResource?WD.BackgroundSolidColorBrush}"?/>
????????<Setter?Property="VerticalContentAlignment"?Value="Center"?/>
????????<Setter?Property="HorizontalContentAlignment"?Value="Stretch"?/>
????????<Setter?Property="Padding"?Value="{StaticResource?WD.DefaultPadding}"?/>
????????<Setter?Property="Template">
????????????<Setter.Value>
????????????????<ControlTemplate?TargetType="{x:Type?controls:IPEditBox}">
????????????????????<ControlTemplate.Resources>
????????????????????????<Style?BasedOn="{StaticResource?WD.DefaultTextBox}"?TargetType="{x:Type?TextBox}">
????????????????????????????<Setter?Property="BorderThickness"?Value="0"?/>
????????????????????????????<Setter?Property="helpers:TextBoxHelper.AllowOnlyNumericInput"?Value="True"?/>
????????????????????????????<Setter?Property="helpers:TextBoxHelper.MaxValue"?Value="255"?/>
????????????????????????????<Setter?Property="helpers:TextBoxHelper.MinValue"?Value="0"?/>
????????????????????????????<Setter?Property="VerticalContentAlignment"?Value="Center"?/>
????????????????????????????<Setter?Property="HorizontalContentAlignment"?Value="Center"?/>
????????????????????????</Style>
????????????????????????<Style?TargetType="TextBlock">
????????????????????????????<Setter?Property="Text"?Value="."?/>
????????????????????????????<Setter?Property="VerticalAlignment"?Value="Center"?/>
????????????????????????</Style>
????????????????????</ControlTemplate.Resources>
????????????????????<Border
????????????????????????x:Name="Root"
????????????????????????Background="{TemplateBinding?Background}"
????????????????????????BorderBrush="{TemplateBinding?BorderBrush}"
????????????????????????BorderThickness="{TemplateBinding?BorderThickness}"
????????????????????????CornerRadius="{Binding?Path=(helpers:ElementHelper.CornerRadius),?RelativeSource={RelativeSource?TemplatedParent}}"
????????????????????????SnapsToDevicePixels="{TemplateBinding?SnapsToDevicePixels}"
????????????????????????UseLayoutRounding="{TemplateBinding?UseLayoutRounding}">
????????????????????????<ScrollViewer?HorizontalScrollBarVisibility="Auto">
????????????????????????????<Grid>
????????????????????????????????<Grid.ColumnDefinitions>
????????????????????????????????????<ColumnDefinition?/>
????????????????????????????????????<ColumnDefinition?Width="Auto"?/>
????????????????????????????????????<ColumnDefinition?/>
????????????????????????????????????<ColumnDefinition?Width="Auto"?/>
????????????????????????????????????<ColumnDefinition?/>
????????????????????????????????????<ColumnDefinition?Width="Auto"?/>
????????????????????????????????????<ColumnDefinition?/>
????????????????????????????????</Grid.ColumnDefinitions>
????????????????????????????????<TextBox?x:Name="PART_TextBox1"?/>
????????????????????????????????<TextBlock?Grid.Column="1"?/>
????????????????????????????????<TextBox?x:Name="PART_TextBox2"?Grid.Column="2"?/>
????????????????????????????????<TextBlock?Grid.Column="3"?/>
????????????????????????????????<TextBox?x:Name="PART_TextBox3"?Grid.Column="4"?/>
????????????????????????????????<TextBlock?Grid.Column="5"?/>
????????????????????????????????<TextBox?x:Name="PART_TextBox4"?Grid.Column="6"?/>
????????????????????????????</Grid>
????????????????????????</ScrollViewer>
????????????????????</Border>
????????????????????<ControlTemplate.Triggers>
????????????????????????<Trigger?Property="IsKeyboardFocused"?Value="True">
????????????????????????????<Setter?Property="BorderBrush"?Value="{DynamicResource?WD.PrimaryNormalSolidColorBrush}"?/>
????????????????????????</Trigger>
????????????????????????<Trigger?Property="IsMouseOver"?Value="True">
????????????????????????????<Setter?Property="BorderBrush"?Value="{DynamicResource?WD.PrimaryNormalSolidColorBrush}"?/>
????????????????????????</Trigger>
????????????????????????<MultiTrigger>
????????????????????????????<MultiTrigger.Conditions>
????????????????????????????????<Condition?Property="IsKeyboardFocused"?Value="True"?/>
????????????????????????????????<Condition?Property="IsMouseOver"?Value="False"?/>
????????????????????????????</MultiTrigger.Conditions>
????????????????????????????<Setter?Property="BorderBrush"?Value="{DynamicResource?WD.PrimaryNormalSolidColorBrush}"?/>
????????????????????????</MultiTrigger>
????????????????????</ControlTemplate.Triggers>
????????????????</ControlTemplate>
????????????</Setter.Value>
????????</Setter>
????</Style>
????<Style?BasedOn="{StaticResource?WD.IPEditBox}"?TargetType="{x:Type?controls:IPEditBox}"?/>
</ResourceDictionary>

效果圖

以上就是基于WPF實現(xiàn)IP輸入控件的詳細內(nèi)容,更多關于WPF IP輸入的資料請關注腳本之家其它相關文章!

相關文章

  • C#代碼實現(xiàn)對AES加密解密

    C#代碼實現(xiàn)對AES加密解密

    這篇文章主要介紹了C#代碼實現(xiàn)對AES加密解密的相關資料,AES是一個新的可以用于保護電子數(shù)據(jù)的加密算法,需要的朋友可以參考下
    2015-12-12
  • C#實現(xiàn)簡易計算器功能(附源碼)

    C#實現(xiàn)簡易計算器功能(附源碼)

    這篇文章主要為大家詳細介紹了C#實現(xiàn)簡易計算器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • C#開發(fā)WinForm之DataGridView開發(fā)詳解

    C#開發(fā)WinForm之DataGridView開發(fā)詳解

    這篇文章主要介紹了C#開發(fā)WinForm之DataGridView開發(fā)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • C#中簡單Socket編程的實現(xiàn)實例

    C#中簡單Socket編程的實現(xiàn)實例

    本文主要介紹了C#中簡單Socket編程的實現(xiàn)實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2025-11-11
  • C#實現(xiàn)啟動項管理的示例代碼

    C#實現(xiàn)啟動項管理的示例代碼

    這篇文章主要為大家詳細介紹了如何利用C#實現(xiàn)啟動項管理,文中的示例代碼講解詳細,對我們學習C#有一定的幫助,感興趣的小伙伴可以了解一下
    2022-12-12
  • C#中fixed關鍵字的作用總結

    C#中fixed關鍵字的作用總結

    以下是對C#中fixed關鍵字的作用進行了詳細的總結介紹,需要的朋友可以過來參考下,希望對大家有所幫助
    2013-09-09
  • Unity實現(xiàn)桌面反彈的示例代碼

    Unity實現(xiàn)桌面反彈的示例代碼

    反彈球是小時候都玩過的網(wǎng)頁小游戲,但是很多人都不知道怎樣實現(xiàn),本文就來介紹一下Unity實現(xiàn)桌面反彈的示例代碼,感興趣的可以了解一下
    2021-05-05
  • C#把文件上傳到服務器中的指定地址

    C#把文件上傳到服務器中的指定地址

    這篇文章介紹了C#實現(xiàn)文件上傳到服務器指定地址的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#實現(xiàn)CSV與PDF和DataTable的快速轉(zhuǎn)換

    C#實現(xiàn)CSV與PDF和DataTable的快速轉(zhuǎn)換

    CSV作為輕量級數(shù)據(jù)載體,在數(shù)據(jù)傳輸中占比超過70%,這篇文章主要為大家介紹了如何使用C#實現(xiàn)CSV與PDF和DataTable的快速轉(zhuǎn)換,需要的可以了解下
    2025-07-07
  • C# 特殊的string類型詳解

    C# 特殊的string類型詳解

    本文主要介紹了C# 特殊的string類型。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-02-02

最新評論

竹北市| 昆山市| 怀集县| 张家口市| 大兴区| 玉田县| 遵化市| 高青县| 开远市| 武强县| 大悟县| 无极县| 临邑县| 高邑县| 绥化市| 武山县| 石家庄市| 苏州市| 靖宇县| 大荔县| 乌审旗| 南澳县| 贵德县| 宁阳县| 鄂尔多斯市| 名山县| 信丰县| 兴仁县| 通化县| 汝南县| 乌审旗| 江都市| 乌拉特后旗| 广南县| 志丹县| 潜江市| 乌审旗| 涿鹿县| 宣化县| 淳化县| 建始县|