C# WPF MVVM 实战 – 5- 用绑定,通过 VM 设置 View 的控件焦点
本文介绍在 MVVM 中,如何用 ViewModel 控制焦点。
这焦点设置个东西嘛,有些争论。就是到底要不要用 ViewModel 来控制视图的键盘输入焦点。这里不讨论,假设你就是要通过 VM,设置输入焦点在哪里。
MSDN 有解释关于 Focus 的,还有 FocusManager,点击这里打开。不知道的话建议你先看看,只求结果的可以直接看下面代码,抄就是了。这次,初级的解释全部略过,直接说做法,看不懂的请留言。做法很多,大概两种比较符合 MVVM 模式:
1. DataTrigger 设置 FocusedElement
Style 中写 Trigger,DataTrigger,值变化触发设置 FocusManager 的 FocusedElement。
XAML:
<Grid><Grid.Style><Style><Style.Triggers><DataTrigger Binding="{Binding Path=ScanFtBarCodeNow}" Value="True"><Setter Property="FocusManager.FocusedElement" Value="{Binding ElementName=FtNoTextBox}"/></DataTrigger></Style.Triggers></Style></Grid.Style><TextBox name="FtNoTextBox"/></Grid>
注意:放置 Style 的位置要注意。一层又一层 Grid 的话,FocusManager 单单设置 FocusedElement 为 TextBox 不足够。原因在 MSDN 有说明,不重复。
2. Attached Property
使用 Attached Property 为控件加属性,值变化触发 CallBack,然后 UIElement.Focus()。所谓值变化,比如,bool 变为 True 。
CODE:
publicstaticclass FocusBehavior {
publicstaticbool GetIsFocused(DependencyObject obj) {
return (bool)obj.GetValue(IsFocusedProperty);
}
publicstaticvoid SetIsFocused(DependencyObject obj, bool value) {
obj.SetValue(IsFocusedProperty, value);
}
publicstaticreadonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused",
typeof(bool),
typeof(FocusBehavior),
new UIPropertyMetadata(false, (s, e) => {
var sender = (UIElement)s;
if ((bool)e.NewValue) {
sender.Focus();
Keyboard.Focus(sender);
}
})
);
}
XAML:
<TextBox local:FocusBehavior.IsFocused="{Binding Path=ScanBarCodeNow}"/>
这做法虽然简单,但实际绑定的并非 UIElement.IsFocused 这只读属性,看CallBack 代码就知道,两个Focus() 都只是方法。界面操作焦点离开此控件时候,DependencyProperty 的值不会变化。后果是,设置一次 true 后,属性值 true,焦点离开它依然是 true,你再设置它 true,会被认定为值无变化,CallBack 没有执行,Focus() 不会运行。Workaround:先设 false 再设 true。
好傻吧。改一下,虽然 IsFocus 是只读无法直接绑定去改它,但可以把 GotFocus 和 LostFocus 事件接上此 Attached Property,触发变更 DependencyProperty 的值,使它变成双向绑定。
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input; namespace Lepton_Practical_MVVM_5 {
publicstaticclass FocusBehavior {
privatestatic Dictionary<UIElement, RoutedEventHandler> handlers =new Dictionary<UIElement, RoutedEventHandler>();
publicstaticbool? GetIsFocused(DependencyObject obj) {
return (bool?)obj.GetValue(IsFocusedProperty);
}
publicstaticvoid SetIsFocused(DependencyObject obj, bool? value) {
obj.SetValue(IsFocusedProperty, value);
}
publicstaticreadonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused",
typeof(bool?),
typeof(FocusBehavior),
new UIPropertyMetadata() {
DefaultValue =null,
PropertyChangedCallback =
(s, e) => {
UIElement sender = (UIElement)s;
RoutedEventHandler x;
if (!handlers.TryGetValue(sender, out x)) {
Attach(sender);
}
if ((bool)e.NewValue) {
sender.Focus();
Keyboard.Focus(sender);
}
}
});
privatestaticvoid Attach(UIElement sender) {
RoutedEventHandler handler = (s, e) => {
UIElement ui = (UIElement)s;
if (e.RoutedEvent == UIElement.GotFocusEvent) {
ui.SetValue(IsFocusedProperty, true);
}
if (e.RoutedEvent == UIElement.LostFocusEvent) {
ui.SetValue(IsFocusedProperty, false);
}
};
sender.GotFocus += handler;
sender.LostFocus += handler;
handlers.Add(sender, handler);
}
}
}
Dictionary 为了记下已连接的 Handler ,以免重复加入。
XAML:
<Window x:Class="Lepton_Practical_MVVM_5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Lepton_Practical_MVVM_5"
Title="MainWindow" Height="350" Width="525"><Grid><TextBox local:FocusBehavior.IsFocused="{Binding Path=IsTextBox1Focus,Mode=TwoWay}" Height="23" HorizontalAlignment="Left" Margin="49,54,0,0" Name="textBox1" VerticalAlignment="Top" Width="120"/></Grid></Window>
点击这里下载Attached Property 版本的代码。
这做法是只有加句柄没有移除,你有情况需要移除的话,自己改吧。
我在这群里,欢迎加入交流:
开发板玩家群 578649319
硬件创客 (10105555)
C# WPF MVVM 实战 – 5- 用绑定,通过 VM 设置 View 的控件焦点的更多相关文章
- 笔记03 wpf 在MVVM模式下怎样在Viewmodel里面获得view的控件对象
转自http://blog.csdn.net/qing2005/article/details/6601199http://blog.csdn.net/qing2005/article/detail ...
- WPF编程,通过Double Animation同时动态缩放和旋转控件的一种方法。
原文:WPF编程,通过Double Animation同时动态缩放和旋转控件的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_4330793 ...
- C# WPF 低仿网易云音乐(PC)Banner动画控件
原文:C# WPF 低仿网易云音乐(PC)Banner动画控件 由于技术有限没能做到一模一样的动画,只是粗略地做了一下.动画有点生硬,还有就是没做出网易云音乐的立体感.代码非常简单粗暴,而且我也写有很 ...
- WPF 程序如何跨窗口/跨进程设置控件焦点
原文:WPF 程序如何跨窗口/跨进程设置控件焦点 WPF 程序提供了 Focus 方法和 TraversalRequest 来在 WPF 焦点范围内转移焦点.但如果 WPF 窗口中嵌入了其他框架的 U ...
- C# WPF MVVM 实战 – 4 - 善用 IValueConverter
IValueConverter,做 WPF 的都应该接触过,把值换成 Visibility .Margin 等等是最常见的例子,也有很多很好的博文解释过用法.本文只是解释一下,MVVM 中一些情景. ...
- wpf mvvm datagrid DataGridTemplateColumn的绑定无效的可能原因之一!
昨天在mvvm wpf的开发中遇到一个问题,绑定不起作用,编辑阶段没问题也没有提示找不到对应的绑定,但是在运行之后却不起作用,查了很多资料,说法不一,有些是要删除datagrid的一行,直接绑定del ...
- WPF DataGrid列设置为TextBox控件的相关绑定
在wpf的DataGrid控件中,某一列的数据模板为TextBox控件的话,绑定Text="{Binding TxtSn, UpdateSourceTrigger=PropertyChang ...
- WPF学习笔记(四):AvalonEdit 代码高亮编辑控件专题
AvalonEdit 是一个基于 WPF 的文本编辑器组件.它是由 Daniel Grunwald 为 SharpDevelop 编写的.从 5.0 版开始,AvalonEdit 根据MIT许可证发布 ...
- WPF系列之二:解耦View层控件事件与ViewModel层事件的响应
以前的做法: 1.当项目的时间比较紧迫的时候,对UI层中控件的事件的处理,往往采取的是类似Winform中最简单的做法,直接做一个事件的Handler直接去调用VM层的方法. 2.控件只有一个Comm ...
随机推荐
- [转]编译Android源代码常见错误解决办法
1. 编译时出现/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.5/../../../libz.so when ...
- android 学习随笔十九(对话框、样式、主题、国际化 )
1.对话框 package com.itheima.dialog; import android.os.Bundle; import android.app.Activity; import andr ...
- 查看linux僵尸进程
top ps -A -o stat,ppid,pid,cmd | grep -e '^[zZ]' kill -HUP 进程号
- selenium验证码处理
在爬虫过程中经常遇到验证码,如何处理验证码就显得很重要 现在来说貌似没有完美的解决方案,很多都是通过第三方平台来实现验证码的验证 将获取的验证码的url发送到第三方平台,接收平台返回的验证码,貌似很简 ...
- Linux查看CPU和内存使用情况【转】
转自:http://www.cnblogs.com/xd502djj/archive/2011/03/01/1968041.html 在系统维护的过程中,随时可能有需要查看 CPU 使用率,并根据相应 ...
- javascript 金额格式化
金额格式化 example: <!DOCTYPE html> <html> <head> <script src="http://code.jque ...
- WIN7(VISTA)系统无法上网问题排查方法
WIN7(VISTA)系统无法上网问题排查方法 一.无法通过DHCP自动获取到IP 1. 确认正确配置路由器的DHCP功能 a.一般租期建议设置为1-3小时,推荐设置1小时. b.DHCP地址池不要和 ...
- weblogic .NoClassDefFoundError: Could not initialize class sun.awt.X11Graphi
这个是常见问题,可以通过增加Weblogic的启动参数来解决: -Djava.awt.headless=true 你可以修改 startWebLogic.sh 文件. export JAVA_OPTI ...
- sqlite3内存不断增加的原因
数据库是这样设计的:用内存保存数据,以提高增删查改的速度,同时把数据写入磁盘,让数据落地. 如果不删除数据库里的数据,随着数据不断地添加到数据库,数据库越来越大,RES内存也越来越大. 见测试代码a. ...
- HDU 2665 && POJ 2104(主席树)
http://poj.org/problem?id=2104 对权值进行建树(这个时候树的叶子是数组b的有序数列),然后二分查找原数列中每个数在有序数列中的位置(即第几小),对每一个前缀[1,i]建一 ...