在WPF中实现一个弹出层自动获取焦点,弹出层实现是通过其UserControl的依赖属性Visibility的绑定实现的,让UserControl上的TextBox获取焦点,初始实现代码如下:

        public Visibility IsVisibile
{
get { return (Visibility)GetValue(IsVisibileProperty); }
set { SetValue(IsVisibileProperty, value); }
}
public static readonly DependencyProperty IsVisibileProperty =
DependencyProperty.Register("IsVisibile", typeof(Visibility), typeof(WordsKeyboard), new PropertyMetadata(Visibility.Collapsed,new PropertyChangedCallback((d,e)=>
{
if((Visibility)e.NewValue==Visibility.Visibile)
(d as MainUserControl).textBox.Focus(); })));

但是第一次弹出该层的时候焦点未在textBox上,除了第一次弹出未获取到焦点后面的弹出都获取到了,为了解决第一次弹出层的TextBox未获取到焦点采用了定时器延时的方法,解决方案如下:

/// <summary>
/// 定义个定时器
/// </summary>
DispatcherTimer timer = new DispatcherTimer();
/// <summary>
/// 构造方法
/// </summary>
public MainUserContrl()
{
InitializeComponent();
timer.Interval = new TimeSpan();
timer.Tick += timer_Tick;
} /// <summary>
/// 定时器间隔执行方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void timer_Tick(object sender, EventArgs e)
{
this.MainText.Focus();
timer.Stop();
}
/// <summary>
/// 控制改用户控件显示隐藏的依赖属性
/// </summary>
public Visibility IsVisibile
{
get { return (Visibility)GetValue(IsVisibileProperty); }
set { SetValue(IsVisibileProperty, value); }
}
public static readonly DependencyProperty IsVisibileProperty =
DependencyProperty.Register("IsVisibile", typeof(Visibility), typeof(WordsKeyboard), new PropertyMetadata(Visibility.Collapsed,new PropertyChangedCallback((d,e)=>
{
if((Visibility)e.NewValue==Visibility.Collapsed)
(d as WordsKeyboard).KillKeyboard();
else
{
(d as MainUserControl).timer.Start();//开启定时器,让textBox获取到焦点
}
})));

通过延时的方法就可以解决掉弹出层第一次无法获取焦点的问题了!而后面让textbox在能使用的情况下不能失去焦点(除非点击弹出层的关闭按钮,让弹出层消失),初始实现代码如下:

  /// <summary>
/// textBox失去焦点事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBox_LostFocus(object sender,RoutedEventArgs e)
{
if(textBox.IsEnabled&&!btnCancle.IsFocused)
textBox.Focus();
}

上述实现,会出现死循环,假如不出现死循环btnCancle.IsFocused的值一直是false,因为取消按钮一直没有获取到焦点!而解决方案实现如下:

 DispatcherTimer timerLoseFocuse = new DispatcherTimer();
timerLoseFocuse.Interval = new TimeSpan(,,,,);
timerLoseFocuse.Tick+=(s,e)=>
{
if(CardNumArea.IsEnabled&&!BtnCancel.IsFocused)
{
this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Render,new Action(()=>
{
t.extBox.Focus();
}));//改方法能解决掉LoseFocuse出现死循环的问题
}
timerLoseFocuse.Stop();
};
/// <summary>
///失去焦点事件
/// </summary>
private void textBox_LostFocus(object sender, RoutedEventArgs e)
{
timerLoseFocuse.Start(); }

通过定时器解决取消按钮无法获取到获取到焦点的问题,否则点击取消无法执行其点击事件

最新文章

  1. DispatcherServlet默认配置
  2. 求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)和不用循环/goto/递归输出1~100的10种写法
  3. Java断言assert
  4. mybatis 关于时间的问题与技巧
  5. 一颗简单的JDBC栗子
  6. JavaScript系统学习小结——变量、作用域和内存问题
  7. sublime列显示控制
  8. stl 和并查集应用
  9. javaweb-1-B/S初论
  10. 神奇的Scala Macro之旅(一)- 什么时候用宏
  11. Mac查看和杀死后台进程
  12. 用python计算圆周率Π
  13. Guava Preconditions 工具参数前置校验
  14. mysql 字段唯一性问题
  15. P1099 树网的核
  16. docker打包centos增加中文支持
  17. 吴裕雄 28-MySQL 序列使用
  18. day22-23作业
  19. 关于720p和1080p观看距离和效果
  20. 警察与小偷的实现之中的一个client与服务端通信

热门文章

  1. Stage3D学习笔记(三):使用GPU绘制一个图片
  2. view 与layer
  3. java正则表达式入门基础
  4. 教你50招提升ASP.NET性能(五):确保分页是在数据层完成的
  5. 【IOS界面布局】横竖屏切换和控件自适应(推荐)
  6. 关于【cocos2dx-3.0beta-制作flappybird】教程在3.2project中出现找不到CCMenuItem.h的解决方法
  7. node.js在windows下的学习笔记(8)---进程管理Process
  8. OpenCV 读取.xml文件
  9. 给定一个字符串,仅由a,b,c 3种小写字母组成。
  10. Android_AnimationDrawable介绍及使用