wpf 两个自定义控件
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位。
工作中有时需要自己做一些自定义控件,用来满足不同场景的需求。两个小控件,比较简单,希望此文能提供一些思路给你。
wpf 两个自定义控件的更多相关文章
- WPF 精修篇 自定义控件
原文:WPF 精修篇 自定义控件 自定义控件 因为没有办法对界面可视化编辑 所以用来很少 现在实现的是 自定义控件的 自定义属性 和自定义方法 用VS 创建自定义控件后 会自动创建 Themes 文件 ...
- WPF中关于自定义控件的滚动条鼠标停留在内容上鼠标滚轮滚动无效的问题
问题起因:在一个用户控件里放置了1个TreeView垂直顺序放置. 当用户控件中的内容超过面板大小时,滚动条会自动出现 ,但是只有当鼠标指示在右边滚动条的那一条位置时,才支持鼠标滚轴滚动. 点在控件内 ...
- WPF两个按钮来回切换样式
<!-- 两个按钮来回切换样式 --> <Style x:Key="SwicthFunctionMetroToggleButton" TargetType=&qu ...
- 解决WPF两个图片控件显示相同图片因线程占用,其中一个显示不全的问题
在做项目的过程中遇到这样一个问题,下面提出一种解决方法,主要思想是图片的Copy,如还有其他方法,欢迎交流. 在前端图片控件绑定显示时,使用转换器进行转义绑定 (1)转换器: public cla ...
- wpf 中的 自定义控件的 binding
XMl 代码 --------------------------------------------------------------------------------------------- ...
- web自定义控件UserControl
今天做了两个自定义控件,之前用WPF也做过,但是感觉跟今天的不太一样.首先是在项目中建了一个UserContral的控件界面,把需要的控件拖到里面,再给按钮添加事件.我们公司的控件都是买的Dev Ex ...
- 自定义WPF 窗口样式
原文:自定义WPF 窗口样式 Normal 0 false 7.8 pt 0 2 false false false EN-US ZH-CN X-NONE 自定义 Window 在客户端程序中,经常需 ...
- C# WPF基础巩固
时间如流水,只能流去不流回. 学历代表你的过去,能力代表你的现在,学习能力代表你的将来. 学无止境,精益求精. 一.写作目的 做C# WPF开发,无论是工作中即将使用,还是只应付跳槽面试,开发基础是非 ...
- 【WPF学习】第六十七章 创建自定义面板
前面两个章节分别介绍了两个自定义控件:自定义的ColorPicker和FlipPanel控件.接下来介绍派生自定义面板以及构建自定义绘图控件. 创建自定义面板是一种特殊但较常见的自定义控件开发子集.前 ...
随机推荐
- C++编译器优化技术:RVO、NRVO和复制省略
现代编译器缺省会使用RVO(return value optimization,返回值优化).NRVO(named return value optimization.命名返回值优化)和复制省略(Co ...
- apt-get原理
apt-get 而这个步骤全要用户亲力亲为可能又有些麻烦,懒是科技发展的重要推动力.所以软件厂商自己编译好了很多二进制文件,只要系统和环境对应,下载之后就能直接安装. 但是如果下载了很多软件我想要管理 ...
- C#中类的修饰符
Q&A 项目=程序集=assembly 1,Q:类的修饰符有哪些? A: 有 new.public.protect.internal.private.abstract.sealed.st ...
- PlayJava Day020
1.异常Exception补充: ①错误(Error)指的是致命性错误,一般无法处理 ②异常以类的形式封装 程序可以处理的异常对应的类是java.lang.Exception及其子类 运行时异常对应的 ...
- 松软科技带你学开发:SQL--FIRST() 函数
FIRST() 函数(原文链接 松软科技:www.sysoft.net.cn/Article.aspx?ID=3731) FIRST() 函数返回指定的字段中第一个记录的值. 提示:可使用 ORDER ...
- OpenCV:图像的颜色空间转换
导包: import numpy as np import cv2 import matplotlib.pyplot as plt def show(image): plt.imshow(image) ...
- elasticsearch failed to obtain node locks
0x00 事件 重启服务器后,启动 elasticsearch 失败,在日志中观察到以下错误: [2019-10-25T17:29:54,639][WARN ][o.e.b.Elasticsearch ...
- Php—使用phpMyAdmin报错
1.Warning in ./libraries/config/FormDisplay.php#661 "continue" targeting switch is equiva ...
- centos7服务器监控之nmon
一.下载nmon 根据系统类型下载相应的版本: http://nmon.sourceforge.net/pmwiki.php?n=Site.Download 目前大多数服务器使用的centos7系统, ...
- s3c2440裸机-UART编程(二、UART编程实现)
UART编程 1.初始化 我们的2440支持3个UART串口,以uart0为例讲解. 那么我们需要实现以下这几个函数完成串口的最基本功能: (1)uart0_init()用于初始化串口 (2)putc ...