wpf怎么获取光标所在控件名称,button按钮向所在控件赋值
1 回答
慕村9548890
TA贡献1884条经验 获得超4个赞
你需要监听button的PreviewGotKeyboardFocus事件,在这个事件处理函数中拿到Keyboard.FocusedElement并变量保存,然后在button的Click事件中就可以使用该变量了,代码大概类似如下:
private IInputElement focusedControl; private void btn_Click(object sender, RoutedEventArgs e) { if(focusedControl != null && focusedControl is TextBox) { TextBox tb = (TextBox)focusedControl; tb.Text = DateTime.Now.ToString(); } } private void btn_PreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { focusedControl = Keyboard.FocusedElement; }
- 1 回答
- 0 关注
- 972 浏览
添加回答
举报
0/150
提交
取消