zoukankan      html  css  js  c++  java
  • WPF User Control Library 制作用户控件库(时间控件)并且引用(一)

    首先创建一个User Control Library工程,工程名为UserTimeEditer

    控件库工程程序集如下

     UserControl1.xaml 前台文件如下:

    <UserControl x:Class="UserTimeEditer.UserControl1"
                 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" 
                 mc:Ignorable="d" 
                 >
        <Border   BorderThickness="1" BorderBrush="#FFABADB3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  >
            <StackPanel   Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" >
                <TextBox  Text="{Binding Hour,StringFormat={}{0:00}}" FontSize="{Binding FontTime}" BorderThickness="0" HorizontalAlignment="Center" VerticalAlignment="Stretch" />
                <TextBlock Text=" : " HorizontalAlignment="Center" FontSize="{Binding FontTime}" VerticalAlignment="Center"/>
                <TextBox  Text="{Binding Minute,StringFormat={}{0:00}}" FontSize="{Binding FontTime}" BorderThickness="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
                <TextBlock Text=" : " HorizontalAlignment="Center" FontSize="{Binding FontTime}" VerticalAlignment="Center"/>
                <TextBox  Text="{Binding Second,StringFormat={}{0:00}}" FontSize="{Binding FontTime}" BorderThickness="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </StackPanel>
        </Border>
    </UserControl>
    UserControl.xaml

    UserControl1.xaml.cs 后台代码如下:

    using System;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Windows.Controls;
    
    namespace UserTimeEditer
    {
        /// <summary>
        /// Interaction logic for UserControl1.xaml
        /// </summary>
        public partial class UserControl1 : UserControl , INotifyPropertyChanged
        {
            public UserControl1()
            {
                InitializeComponent();
                DataContext = this;
            }
    
            private int _fontTime = 0;
            public int FontTime
            {
                get { return _fontTime; }
                set { _fontTime = value; NotifyPropertyChanged(); }
            }
            private int _Hour = 0;
            public int Hour
            {
                get { return _Hour; }
                set { _Hour = value; NotifyPropertyChanged(); }
            }
    
            private int _Minute = 0;
            public int Minute
            {
                get { return _Minute; }
                set { _Minute = value; NotifyPropertyChanged(); }
            }
    
            private int _Second = 0;
            public int Second
            {
                get { return _Second; }
                set { _Second = value; NotifyPropertyChanged(); }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    }
    UserControl1.xaml.cs

     编译该控件库程序集,编译成功生成UserTimeEditer.dll动态链接库

    再创建一个WPF App工程来测试一下,工程名为:TestUserTimeEditer

    整体项目如下:

     右击App程序集的依赖项,点击添加引用

    在项目->解决方案下找到刚才生成的UserTimeEditer.dll动态链接库,点击确定添加进来

     

     在工具箱内就能找到刚才生成的时间控件,此时就能把该控件拖进MainWindow.xaml布局器中

    MainWindow.xaml前台文件如下:

    <Window
            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:TestUserTimeEditer"
            xmlns:UserTimeEditer="clr-namespace:UserTimeEditer;assembly=UserTimeEditer" x:Class="TestUserTimeEditer.MainWindow"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <StackPanel>
            <UserTimeEditer:UserControl1 x:Name="TimeEditer" FontTime="30">
            </UserTimeEditer:UserControl1>
            <Button Content="读时间" Click="btnClickReadTime"/>
        </StackPanel>
    </Window>
    MainWindow.xaml

    MainWindow.xam.cs后台代码如下:

    using System.Windows;
    
    namespace TestUserTimeEditer
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void btnClickReadTime(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("时间为: " + TimeEditer.Hour.ToString("00") + " : " + TimeEditer.Minute.ToString("00") + " : " + TimeEditer.Second.ToString("00"));
            }
    
        }
    }
    MainWindow.xaml.cs

    运行结果如下:

    下一篇博客进一步的使用该控件:https://www.cnblogs.com/lizhiqiang0204/p/13530633.html

  • 相关阅读:
    团队博客-会议之初
    5.2 个人作业2
    5.1 如何利用Intellij Idea搭建python编译运行环境
    4.27 python神器——Anaconda的安装与优化配置
    4.26 AlertDialog(对话框)详解
    4.25 xmapp启动mysql出现Error: MySQL shutdown unexpectedly.
    4.24 Android Studio下应用签名的方法以及获取 MD5、SHA1(签名)、SHA256 值
    4.23 2020.2新版本idea创建javaEE web文件
    4.22 Android studio 通过获取验证码用户登陆成功
    4.21 Android 记住密码和自动登录界面的实现(SharedPreferences 的用法)
  • 原文地址:https://www.cnblogs.com/lizhiqiang0204/p/13458840.html
Copyright © 2011-2022 走看看