wpf创建用户控件(计时器控件)
在vs中新增用户控件

前台xaml如下代码:
<UserControl x:Class="Zh.SelfServiceEquipment.UI.ZhControls.CountDownTimeControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Zh.SelfServiceEquipment.UI.ZhControls"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="300"
Loaded="UserControl_Loaded">
<Grid>
<StackPanel Orientation="Horizontal">
<Label Content="倒计时" Foreground="White" FontSize="20" VerticalAlignment="Center"></Label>
<Label Content="30" Foreground="Red" FontSize="25" VerticalAlignment="Center" x:Name="lblTime"></Label>
<Label Content="秒" Foreground="White" FontSize="20" VerticalAlignment="Center"></Label>
</StackPanel>
</Grid>
</UserControl>
CS代码:
public partial class CountDownTimeControl : UserControl
{
public delegate void CountDownTimeOutEventHandler(object sender);
public event CountDownTimeOutEventHandler OnCountDownTime;
private System.Windows.Threading.DispatcherTimer dTime;//定义事件,在倒计时结束的时候进行调用
public CountDownTimeControl()
{
InitializeComponent();
StartCountdownTime();
}
private int _count; public int Count//定义时间,并将该时间的值赋值给前台页面
{
get { return _count; }
set { _count = value; this.lblTime.Content = _count; }
}
public void StartCountdownTime() {
dTime = new System.Windows.Threading.DispatcherTimer();
dTime.Interval = new TimeSpan(,,);
dTime.Tick += DTime_Tick;
}
private void DTime_Tick(object sender, EventArgs e)
{
if (Count-- == )
{
this.dTime.Stop();
if (OnCountDownTime!=null)
{
this.OnCountDownTime(this);
}
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
dTime.Start();
}
}
接下来就是在MainWindow.xaml文件中对该控件进行引用
在前台页面命名空间写入
xmlns:zhControls="clr-namespace:Zh.SelfServiceEquipment.UI.ZhControls"
其中zhControls是随便定义的,Zh.SelfServiceEquipment.UI.ZhControls是项目中用户控件所在的命名空间
MainWindows.xaml前台代码如下
<Window x:Class="Zh.SelfServiceEquipment.UI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Zh.SelfServiceEquipment.UI"
xmlns:zhControls="clr-namespace:Zh.SelfServiceEquipment.UI.ZhControls"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid Background="Black">
<WrapPanel>
<zhControls:CountDownTimeControl Count="10" OnCountDownTime="CountDownTimeControl_OnCountDownTime"></zhControls:CountDownTimeControl>
</WrapPanel>
</Grid>
</Window>
MainWindow.xaml.cs代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CountDownTimeControl_OnCountDownTime(object sender)
{
MessageBox.Show("倒计时结束");
}
}
大功告成,让我们看看运行结果


wpf创建用户控件(计时器控件)的更多相关文章
- WPF 创建用户控件并引用
项目源码地址:https://github.com/lizhiqiang0204/WpfControlLibrary.git 首先创建新项目->WPF用户控件库项目 在UserControl1. ...
- winform用户控件、动态创建添加控件、timer控件、控件联动
用户控件: 相当于自定义的一个panel 里面可以放各种其他控件,并可以在后台一下调用整个此自定义控件. 使用方法:在项目上右键.添加.用户控件,之后用户控件的编辑与普通容器控件类似.如果要在后台往窗 ...
- 浅尝辄止WPF自定义用户控件(实现颜色调制器)
主要利用用户控件实现一个自定义的颜色调制控件,实现一个小小的功能,具体实现界面如下. 首先自己新建一个wpf的用户控件类,我就放在我的wpf项目的一个文件夹下面,因为是一个很小的东西,所以就没有用mv ...
- 在用户控件(ASCX)创建用户控件(ASCX)
"我建了两个ascx,ascxA,ascxBascxA中放了一个PlaceHold,ascxB中放了一个textBoxascxA在page_load中动态创建了5个ascxB但是页面上什么都 ...
- WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如 WPF中不规则窗体与WebBrowser控件的兼 ...
- WPF:理解ContentControl——动态添加控件和查找控件
WPF:理解ContentControl--动态添加控件和查找控件 我认为WPF的核心改变之一就是控件模型发生了重要的变化,大的方面说,现在窗口中的控件(大部分)都没有独立的Hwnd了.而且控件可以通 ...
- Git使用总结 Asp.net生命周期与Http协议 托管代码与非托管代码的区别 通过IEnumerable接口遍历数据 依赖注入与控制反转 C#多线程——优先级 AutoFac容器初步 C#特性详解 C#特性详解 WPF 可触摸移动的ScrollViewer控件 .NET(C#)能开发出什么样的APP?盘点那些通过Smobiler开发的移动应用
一,原理 首先,我们要明白Git是什么,它是一个管理工具或软件,用来管理什么的呢?当然是在软件开发过程中管理软件或者文件的不同版本的工具,一些作家也可以用这个管理自己创作的文本文件,由Linus开发的 ...
- WPF进阶技巧和实战03-控件(1-控件及内容控件)
所有控件都继承自System.Windows.Controls.Control类,这个类添加一些基本结构: 设置控件内容对齐方式 (HorizontalContentAlignment,Vertica ...
- WPF布局之让你的控件随着窗口等比放大缩小,适应多分辨率满屏填充应用
一直以来,我们设计windows应用程序,都是将控件的尺寸定好,无论窗体大小怎么变,都不会改变,这样的设计对于一般的应用程序来说是没有问题的,但是对于一些比较特殊的应用,比如有背景图片的,需要铺面整个 ...
随机推荐
- HDU1124 Factorial
Problem Description The most important part of a GSM network is so called Base Transceiver Station ( ...
- 初识RabbitMQ系列之一:简单介绍
一:RabbitMQ是什么? 众所周知,MQ是Message Queue(消息队列)的意思,RabbitMQ就是众多MQ框架其中的一款,开源实现了AMQP协议(官网:http://www.amqp. ...
- vim 配置插件
vim插件可以用bundle管理,我这里面用的是一个开源的Vundle工具,git上操作步骤说的很清楚https://github.com/VundleVim/Vundle.vim 之前PluginI ...
- Android学习笔记-EditText(输入框)(二)
6.控制EditText四周的间隔距离与内部文字与边框间的距离 我们使用margin相关属性增加组件相对其他控件的距离,比如android:marginTop = "5dp" 使用 ...
- nginx响应高并发参数配置
一.一般来说nginx 配置文件中对优化比较有作用的为以下几项: 1. worker_processes 8; nginx 进程数,建议按照cpu 数目来指定,一般为它的倍数 (如,2个四核的cpu ...
- CentOS6.3 下安装codeblocks
本人用的系统是centos6.3(虚拟机) 需要预先安装gcc编译器(参考:http://www.cnblogs.com/magialmoon/archive/2013/05/05/3061108.h ...
- ps 命令的详细功能解析
转自:http://www.cnblogs.com/wangkangluo1/archive/2011/09/23/2185938.html 要对进程进行监测和控制,首先必须要了解当前进程的情况,也就 ...
- HTML的学习笔记
一.HTML的概述(了解) a.html是什么 : hypertext markup language 超文本标记语言 超文本:音频,视频,图片称为超文本.. ...
- MySQL grant命令使用
MySQL 赋予用户权限命令的简单格式可概括为: grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. grant sele ...
- 一些实用而又记不住的css技巧
user-select 禁止用户选中文本 div { user-select: none; /* Standard syntax */ } 清除手机tap事件后element 时候出现的一个高亮 * ...