wpf 兩個自定義控制項 一個是IP控制項,一個滑動條。先看下效果圖 IPControl 1、實際工作中有時需要設置IP信息,就想著做一個ip控制項。效果沒有window自帶的好,需要通過tab切換。但也能滿足使用。廢話不多說直接上代碼 IPControl.xaml IPControl.xaml.cs 2 ...
wpf 兩個自定義控制項
一個是IP控制項,一個滑動條。先看下效果圖
IPControl
1、實際工作中有時需要設置IP信息,就想著做一個ip控制項。效果沒有window自帶的好,需要通過tab切換。但也能滿足使用。廢話不多說直接上代碼
IPControl.xaml
<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>
IPControl.xaml.cs
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
}
2、控制項有4個TextBox、4個Label組成。TextBox顯示IP值,Label顯示IP數據的“.”。
TextBox綁定依賴屬性,設置TabIndex參數,通過Tab按鍵切換到下一個TextBox。每個TextBox最多輸入3位
LightControl
1、前段時間,領導緊急安排一個工作。做一個測試燈光的小軟體。與負責燈光同事溝通得知,光源板可同時控制24路燈。也就是說軟體界面上需要有24個ScrollBar用來表示燈光亮度,24個Label顯示名稱。這要是一個一個控制項加太慢了,沒法做一個自定義空間,可設置顯示名稱,通過滑動條或者直接設置參數,改變亮度。於是需要一個Label、一個ScrollBar、一個TextBox與ScrollBar關聯。
LightControl.xaml
<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>
LightControl.xaml.cs
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
}
2、Label顯示名稱通過依賴屬性由外接傳入。
3、ScrollBar的Value屬性與TextBox的Text屬性綁定同一個依賴屬性,可傳遞到調用者,同時TextBox顯示信息設置保留小數點4位。
工作中有時需要自己做一些自定義控制項,用來滿足不同場景的需求。兩個小控制項,比較簡單,希望此文能提供一些思路給你。