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控件.接下来介绍派生自定义面板以及构建自定义绘图控件. 创建自定义面板是一种特殊但较常见的自定义控件开发子集.前 ...
随机推荐
- shell中字典的一个用法示例
1. shell中字典的用法示例, 数组用法同字典 2. 以上使用sell配合字典实现的功能, 使用awk能轻松搞定, 如下: awk '{print $2}' file.txt | sort | u ...
- Java描述设计模式(02):简单工厂模式
本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景简介 1.引入场景 订餐流程简单描述 1).食品抽象类,规定食品的基础属性操作 2).鱼类,鸡肉类食品类扩展 3).订餐流程类,根 ...
- django.db.utils.OperationalError: (1093, "You can't specify target table 'xxx' for update in FROM clause")
这个错误的意思是,不能在update某张表的where条件中,再次select这张表的某些值作为筛选条件,比如: update message set content = "hello&qu ...
- SpringBoot中maven打包,启动报没有主清单属性
有时候会出现这种情况,看一下项目的pom中是否有这个插件配置,没有的话需要引入. <build> <plugins> <plugin> <groupId> ...
- C#函数的递归
using System; namespace ConsoleApp3 { class Program { static void Main(string[] args) { ); Console.W ...
- 原生PHP网页导出和导入excel文件实例
原生PHP实现的网页导出和导入excel文件实例,包括上传也是用的原生.还可在exportExcel方法里设置字体等表格样式. 导出和导入表单代码: <p style="margin: ...
- SpringCloud(八):springcloud-bus消息总线(刷新配置服务)
Bus消息总线: 好了现在我们接着上一篇的随笔,继续来讲.上一篇我们讲到,我们如果要去更新所有微服务的配置,在不重启的情况下去更新配置,只能依靠spring cloud config了,但是,是我们要 ...
- java学习第一步-工欲善其事必先利其器
Java SE 磨刀不误砍柴工,工欲善其事必先利其器,咱们先搞好硬件配置,才能顺利的搞好Java学习 阶段一 1.认识Java 2.java发展史及用户 3.配置Java环境 4.JDK8下载安装 5 ...
- SSM框架之Spring(2)IOC及依赖注入
Spring(2)IOC及依赖注入 基于xml配置文件的实现 1.IOC (控制反转-Inversion Of Control) 控制反转(Inversion of Control,缩写为IoC),是 ...
- Android Gradle 学习笔记(四):Gradle 构建脚本
本节我们从整体的角度来介绍一下Gradle. 一.setting.gradle 在Gradle中,定义了一个设置文件,用于初始化以及工程树的配置.设置文件的默认的名字就是setting.gradle, ...