博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
wpf之Popup弹出自定义输入"键盘"
阅读量:6296 次
发布时间:2019-06-22

本文共 7368 字,大约阅读时间需要 24 分钟。

    在很多工厂的信息化MES系统中,车间的采集数据的机器是触摸屏电脑(工厂环境所限,用外接鼠标键盘反而不方便)。

由于没有外接键盘,所以用户无法像坐在办公室一样,用鼠标键盘进行录入信息。

这时我们可以用wpf的Popup控件,当点击一个"文本框"时,弹出一个自定义的键盘(UserControl),实现如下图效果:

 

自定义用户控件(UserControl),"键盘":

Xaml:

View Code

 

C#:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace PopupDemo{    ///     /// NumericKeyboard.xaml 的交互逻辑    ///     public partial class NumericKeyboard : UserControl    {        public NumericKeyboard()        {            InitializeComponent();        }        public bool HasMaxValue        {            get { return (bool)GetValue(HasMaxValueProperty); }            set { SetValue(HasMaxValueProperty, value); }        }        // Using a DependencyProperty as the backing store for HasMaxValue.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty HasMaxValueProperty =            DependencyProperty.Register("HasMaxValue", typeof(bool), typeof(NumericKeyboard), new UIPropertyMetadata(true));        public int MaxValue        {            get { return (int)GetValue(MaxValueProperty); }            set { SetValue(MaxValueProperty, value); }        }        // Using a DependencyProperty as the backing store for MaxValue.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty MaxValueProperty =            DependencyProperty.Register("MaxValue", typeof(int), typeof(NumericKeyboard), new UIPropertyMetadata(0));        public int Value        {            get { return (int)GetValue(ValueProperty); }            set { SetValue(ValueProperty, value); }        }        // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty ValueProperty =            DependencyProperty.Register("Value", typeof(int), typeof(NumericKeyboard), new UIPropertyMetadata(0));        public bool IsChecked        {            get { return (bool)GetValue(IsCheckedProperty); }            set { SetValue(IsCheckedProperty, value); }        }        // Using a DependencyProperty as the backing store for IsChecked.  This enables animation, styling, binding, etc...        public static readonly DependencyProperty IsCheckedProperty =            DependencyProperty.Register("IsChecked", typeof(bool), typeof(NumericKeyboard), new UIPropertyMetadata(false));        private void btnMin_Click(object sender, RoutedEventArgs e)        {            //if (Item != null && Item.Quantity1 > 0)            //{            //    Item.Quantity2 = Item.Quantity1;            //}            Value = 0;        }        private void AddNumber(int num)        {            //if (Item != null)            //{            //    int cnt = Item.Quantity2 * 10 + num;            //    if (Item.Quantity1 > 0 && cnt > Item.Quantity1)            //    {            //        Item.Quantity2 = Item.Quantity1;            //    }            //    else            //    {            //        Item.Quantity2 = cnt;            //    }            //}            if (HasMaxValue)            {                Value = Math.Min(MaxValue, Value * 10 + num);            }            else            {                Value = Value * 10 + num;            }        }        private void button2_Click(object sender, RoutedEventArgs e)        {            AddNumber(2);        }        private void button3_Click(object sender, RoutedEventArgs e)        {            AddNumber(3);        }        private void button4_Click(object sender, RoutedEventArgs e)        {            AddNumber(4);        }        private void button5_Click(object sender, RoutedEventArgs e)        {            AddNumber(5);        }        private void button6_Click(object sender, RoutedEventArgs e)        {            AddNumber(6);        }        private void button7_Click(object sender, RoutedEventArgs e)        {            AddNumber(7);        }        private void button8_Click(object sender, RoutedEventArgs e)        {            AddNumber(8);        }        private void button9_Click(object sender, RoutedEventArgs e)        {            AddNumber(9);        }        private void button0_Click(object sender, RoutedEventArgs e)        {            AddNumber(0);        }        private void button1_Click(object sender, RoutedEventArgs e)        {            AddNumber(1);        }        private void btnClose_Click(object sender, RoutedEventArgs e)        {            IsChecked = false;        }    }}
View Code

 

主窗体:

Xaml:

View Code

 

C#:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;using System.ComponentModel;namespace PopupDemo{    ///     /// MainWindow.xaml 的交互逻辑    ///     public partial class MainWindow : Window,INotifyPropertyChanged    {        private int terminalNo;        public int TerminalNo        {            get { return terminalNo; }            set             {                if(value!=terminalNo)                {                    terminalNo = value;                    OnPropertyChanged("TerminalNo");                }            }        }                public MainWindow()        {            InitializeComponent();        }        public event PropertyChangedEventHandler PropertyChanged;        public void OnPropertyChanged(string propertyName)        {            if (this.PropertyChanged != null)            {                this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));            }        }        private void btnShow_Click(object sender, RoutedEventArgs e)        {            MessageBox.Show(string.Format("终端编号的值为{0}", TerminalNo));        }    }}
View Code

 

验证输入后,绑定TextBlock的Text属性上值是否真的变化了:

 

总结:

1.Popup控件永远不会自动显示,为了显示Popup控件必须设置IsOpen属性。

2.默认情况下,Popup.StaysOen属性被设置为True,并且Popup控件会一直显示,直到显式地将IsOpen属性设置为False;

  如果将Popup.StaysOpen属性设置为False,当用户在其他地方单击鼠标时,Popup效果关闭。

如果Popup控件的IsOpen属性设置为True时,通过Popup控件的PopupAnimation属性(同时必须要设置属性 AllowsTransparency="True")

可以设置Popup控件的显示方式:

转载于:https://www.cnblogs.com/527289276qq/p/5386766.html

你可能感兴趣的文章
css面试题
查看>>
Vue组建通信
查看>>
用CSS画一个带阴影的三角形
查看>>
前端Vue:函数式组件
查看>>
程鑫峰:1.26特朗.普力挺美元力挽狂澜,伦敦金行情分析
查看>>
safari下video标签无法播放视频的问题
查看>>
01 iOS中UISearchBar 如何更改背景颜色,如何去掉两条黑线
查看>>
对象的继承及对象相关内容探究
查看>>
Spring: IOC容器的实现
查看>>
Serverless五大优势,成本和规模不是最重要的,这点才是
查看>>
Nginx 极简入门教程!
查看>>
iOS BLE 开发小记[4] 如何实现 CoreBluetooth 后台运行模式
查看>>
Item 23 不要在代码中使用新的原生态类型(raw type)
查看>>
为网页添加留言功能
查看>>
JavaScript—数组(17)
查看>>
Android 密钥保护和 C/S 网络传输安全理论指南
查看>>
以太坊ERC20代币合约优化版
查看>>
Why I Began
查看>>
同一台电脑上Windows 7和Ubuntu 14.04的CPU温度和GPU温度对比
查看>>
js数组的操作
查看>>