wpf 兩個自定義控件c#
一個是IP控件,一個滑動條。先看下效果圖測試
一、實際工做中有時須要設置IP信息,就想着作一個ip控件。效果沒有window自帶的好,須要經過tab切換。但也能知足使用。廢話很少說直接上代碼spa
<UserControl x:Class="WpfApp1.IPControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1"> <Viewbox> <Border> <DockPanel> <DockPanel.Resources> <Style TargetType="{x:Type TextBox}"> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Top"/> <Setter Property="TextAlignment" Value="Center"/> <Setter Property="BorderThickness" Value="0"/> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="VerticalAlignment" Value="Stretch"/> <Setter Property="MaxLength" Value="3"/> <Setter Property="Width" Value="30"/> <Setter Property="Height" Value="25"/> </Style> <Style TargetType="{x:Type Label}"> <Setter Property="Content" Value="."/> </Style> </DockPanel.Resources> <TextBox TabIndex="1" GotFocus="TextBoxGotFocus" Text="{Binding Path=FirstIPValue, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:IPControl}}, UpdateSourceTrigger=PropertyChanged}"/> <Label/> <TextBox TabIndex="2" GotFocus="TextBoxGotFocus" Text="{Binding Path=SecondIPValue, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:IPControl}}, UpdateSourceTrigger=PropertyChanged}"/> <Label/> <TextBox TabIndex="3" GotFocus="TextBoxGotFocus" Text="{Binding Path=ThirdIPValue, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:IPControl}}, UpdateSourceTrigger=PropertyChanged}"/> <Label/> <TextBox TabIndex="4" GotFocus="TextBoxGotFocus" Text="{Binding Path=ForthIPValue, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:IPControl}}, UpdateSourceTrigger=PropertyChanged}"/> </DockPanel> </Border> </Viewbox> </UserControl>
public partial class IPControl : UserControl { public IPControl() { InitializeComponent(); } #region DependencyProperty public static readonly DependencyProperty IPAddressProperty = DependencyProperty.Register("IPAddress", typeof(string), typeof(IPControl), new PropertyMetadata(defaultIP, (d, e) => { if (d is IPControl control) { control.UpdateParts(control); } })); public string IPAddress { get { return (string)GetValue(IPAddressProperty); } set { SetValue(IPAddressProperty, value); } } #endregion #region Static Field private static readonly string defaultIP = "127.0.0.1"; #endregion #region Field private string firstIPValue; private string secondIPValue; private string thirdIPValue; private string forthIPValue; #endregion #region Property public string FirstIPValue { get { return firstIPValue; } set { if (firstIPValue != value) { UpdateIPText(value, 1, ref firstIPValue); } } } public string SecondIPValue { get { return secondIPValue; } set { if (secondIPValue != value) { UpdateIPText(value, 0, ref secondIPValue); } } } public string ThirdIPValue { get { return thirdIPValue; } set { if (thirdIPValue != value) { UpdateIPText(value, 0, ref thirdIPValue); } } } public string ForthIPValue { get { return forthIPValue; } set { if (forthIPValue != value) { UpdateIPText(value, 0, ref forthIPValue); } } } #endregion #region Private Method private void TextBoxGotFocus(object sender, RoutedEventArgs e) { InputMethod.Current.ImeState = InputMethodState.Off; TextBox tb = sender as TextBox; if (tb.Text.Length != 0) { tb.SelectAll(); } } private void UpdateIPText(string oldvalue, int minValue, ref string newValue) { int.TryParse(oldvalue, out int iValue); if (iValue < minValue) { iValue = minValue; } if (iValue > 255) { iValue = 255; } newValue = iValue.ToString(); IPAddress = GetIPAddress(); } private string GetIPAddress() { string str = ""; if (firstIPValue != null && firstIPValue.Length > 0) { str += firstIPValue + "."; } else { str += "0."; } if (secondIPValue != null && secondIPValue.Length > 0) { str += secondIPValue + "."; } else { str += "0."; } if (thirdIPValue != null && thirdIPValue.Length > 0) { str += thirdIPValue + "."; } else { str += "0."; } if (forthIPValue != null && forthIPValue.Length > 0) { str += forthIPValue; } else { str += "0"; } return str; } private void UpdateParts(IPControl control) { if (control.IPAddress == null) { control.IPAddress = defaultIP; } string[] parts = control.IPAddress.Split('.'); if (parts.Length == 4) { control.FirstIPValue = parts[0]; control.SecondIPValue = parts[1]; control.ThirdIPValue = parts[2]; control.ForthIPValue = parts[3]; } } #endregion }
二、控件有4個TextBox、4個Label組成。TextBox顯示IP值,Label顯示IP數據的「.」。3d
TextBox綁定依賴屬性,設置TabIndex參數,經過Tab按鍵切換到下一個TextBox。每一個TextBox最多輸入3位code
一、前段時間,領導緊急安排一個工做。作一個測試燈光的小軟件。與負責燈光同事溝通得知,光源板可同時控制24路燈。也就是說軟件界面上須要有24個ScrollBar用來表示燈光亮度,24個Label顯示名稱。這要是一個一個控件加太慢了,無法作一個自定義空間,可設置顯示名稱,經過滑動條或者直接設置參數,改變亮度。因而須要一個Label、一個ScrollBar、一個TextBox與ScrollBar關聯。orm
<UserControl x:Class="WpfApp1.LightControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1"> <Viewbox> <Border> <DockPanel> <Label Content="{Binding Path=LabelContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:LightControl}}}" FontSize="17" Width="80" VerticalContentAlignment="Center"/> <ScrollBar Value="{Binding Path=LightValue, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:LightControl}}}" Orientation="Horizontal" Height="40" Width="200" Maximum="100" SmallChange="1"/> <TextBox Text="{Binding Path=LightValue, StringFormat={}{0:F4}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:LightControl}}, UpdateSourceTrigger=PropertyChanged}" FontSize="17" Width="80" VerticalContentAlignment="Center"/> </DockPanel> </Border> </Viewbox> </UserControl>
public partial class LightControl : UserControl { public LightControl() { InitializeComponent(); } #region DependencyProperty public static readonly DependencyProperty LabelContentProperty = DependencyProperty.Register("LabelContent", typeof(string), typeof(LightControl), new PropertyMetadata("燈光")); public string LabelContent { get { return (string)GetValue(LabelContentProperty); } set { SetValue(LabelContentProperty, value); } } public static readonly DependencyProperty LightValueProperty = DependencyProperty.Register("LightValue", typeof(double), typeof(LightControl), new PropertyMetadata(1.0)); public double LightValue { get { return (double)GetValue(LightValueProperty); } set { SetValue(LightValueProperty, value); } } #endregion }
二、Label顯示名稱經過依賴屬性由外接傳入。xml
三、ScrollBar的Value屬性與TextBox的Text屬性綁定同一個依賴屬性,可傳遞到調用者,同時TextBox顯示信息設置保留小數點4位。blog
工做中有時須要本身作一些自定義控件,用來知足不一樣場景的需求。兩個小控件,比較簡單,但願此文能提供一些思路給你。ip