[Silverlight]监听指定控件(FrameworkElement)的依赖属性(DependencyProperty)的更改
前言
转载请注明出处:http://www.cnblogs.com/ainijiutian
最近在silverlight项目使用Telerik的控件,遇到一个问题。就是使用RadBusyIndicator,当IsBusy = false时,其内的控件(以TextBox为例)的焦点会丢失。IsBusy绑定的是ViewModel的IsBusy属性,Button点击时调用ViewModel的异步耗时方法,耗时方法结束时设置IsBusy = false,再调用回调函数。在回调函数调用txtInput.Focus()。
1: <telerik:RadBusyIndicator x:Name="rbiBusy" IsBusy="{Binding IsBusy}">
2: <telerik:RadButton Content="Button" Name="button1" Click="button1_Click" />
3: <TextBox x:Name="txtInput" />
4: </telerik:RadBusyIndicator>
看样子是没什么问题的,但是实际情况是文本框不会获取到焦点。经过跟踪也发现txtInput.Focus()返回false。
正文
在谷歌狂搜,才发现RadBusyIndicator的IsBusy不靠谱,IsBusyIndicationVisible才是好孩子。反编译也证明了这点,IsBusy设置为false时,在 AnimationManager.Play函数的回调里设置IsBusyIndicationVisible=false。不过IsBusyIndicationVisible定义的是private set,程序里不太好监控。官方论坛有一种解决方案,是定义一个有附加属性的类,然后在文本框上增加local:Helper.EnsureFocus="{Binding IsBusyIndicationVisible, ElementName=rbiBusy}"。
1: public class Helper
2: {
3: private static void OnEnsureFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
4: {
5: if (!(bool)e.NewValue)
6: {
7: (d as Control).Focus();
8: }
9: }
10:
11: public static bool GetEnsureFocus(DependencyObject obj)
12: {
13: return (bool)obj.GetValue(EnsureFocusProperty);
14: }
15:
16: public static void SetEnsureFocus(DependencyObject obj, bool value)
17: {
18: obj.SetValue(EnsureFocusProperty, value);
19: }
20:
21: // Using a DependencyProperty as the backing store for EnsureFocus. This enables animation, styling, binding, etc...
22: public static readonly DependencyProperty EnsureFocusProperty =
23: DependencyProperty.RegisterAttached("EnsureFocus", typeof(bool), typeof(Helper), new PropertyMetadata(OnEnsureFocusChanged));
24: }
不过发现不能通用到其他场景。后来又发现一牛人2009年的帖子,才最终解决。
结语
整理如下。有兴趣可以做成扩展方法。
1: public void ListenPropertyChanged(FrameworkElement element, string propertyName, PropertyChangedCallback callback)
2: {
3: Binding b = new Binding(propertyName) { Source = element }; //http://www.cnblogs.com/ainijiutian
4: var prop = System.Windows.DependencyProperty.RegisterAttached("ListenAttached" + propertyName, typeof(object), typeof(FrameworkElement), new System.Windows.PropertyMetadata(callback));
5: element.SetBinding(prop, b);
6: }
使用
1: ListenPropertyChanged("IsBusyIndicationVisible", rbiBusy, (d, a) =>
2: {
3: if (!(bool)a.NewValue)
4: {
5: txtInput.Focus();
6: }
7: });
[Silverlight]监听指定控件(FrameworkElement)的依赖属性(DependencyProperty)的更改的更多相关文章
- FineReport——JS监听其他控件事件
首先在参数面板和报表中分布添加一个button,用于被监听: 参数面板 控件名:temp: temp点击事件:alert("temp"); 报表 控件名:exprt: temp点击 ...
- Silverlight代码编写对控件的PlaneProjection.RotationY属性控制动画
Canvas c; void btnDraw_Click(object sender, RoutedEventArgs e) { Storyboard story = new Storyboard() ...
- WPF 用户控件的自定义依赖属性在 MVVM 模式下的使用备忘
依赖属性相当于扩充了 WPF 标签的原有属性列表,并可以使用 WPF 的绑定功能,可谓是十分方便的:用户控件则相当于代码重用的一种方式:以上几点分开来还是比较好理解的,不过要用到MVVM 模式中,还是 ...
- WinForm 批量设置指定控件中的控件状态
在开发中常遇到当点击某个按钮的时候,禁用文本框或按钮的的状态,以防止误操作,下面的代码是我已批量设置指定控件中的按钮状态的代码,同理可以延伸出很多操作. /// <summary> /// ...
- 监听指定端口数据交互(HttpListenerContext )
很怀念以前做机票的日子,,,,可惜回不去 以前的项目中的,拿来贴贴 场景:同步第三方数据,监听指定地址(指定时间间隔,否则不满足,因为需要处理粘包问题,改篇未实现) 主要内容四个文件:下面分别说下每个 ...
- 使用path监听指定文件系统的变化
在以前的JAVA版本中,如果程序需要检测文件的变化,那么需要开辟一个线程每隔一段时间去遍历一次指定的目录,如果发现此次遍历结果和上次不同,那么就认为文件变动了 ,这样的方式非常繁琐,JAVA 7之后的 ...
- GridView绑定数据与隐藏指定控件(模板列)
1.1. GridView绑定数据 1) 可以配置SqlDataSource数据源,修改select语句生成框架(不想手动绑定) 2) 删除DataSourceID属性和 ...
- 基本控件文档-UITextField属性
CHENYILONG Blog 基本控件文档-UITextField属性 Fullscreen UITextField属性技术博客http://www.cnblogs.com/ChenYilong ...
- 基本控件文档-UISegment属性
CHENYILONG Blog 基本控件文档-UISegment属性 Fullscreen UISegment属性技术博客http://www.cnblogs.com/ChenYilong/ 新浪 ...
随机推荐
- jQuery的封装方式与JS中new的实现原理
function jQuery() { return new jQuery.fn.init(); } jQuery.fn = jQuery.prototype = { init: function() ...
- PNG和Gif及JPEG图片格式比较
Gif格式特点 透明性Gif是一种布尔透明类型,既它可以是全透明,也可以是全不透明,但是它并没有半透明(alpha 透明). 动画Gif这种格式支持动画. 无损耗性Gif是一种无损耗的图像格式,这也意 ...
- Final-阶段站立会议6
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- php多线程详解
在说明多线程的题前,需要弄清楚以下几个问题 1,ts 和 nts的区别 Thread Safe和NoneThread Safe 先说windows的,在php官网,在windows区域有在文件下在有 ...
- 10个jQuery小技巧
收集的10个 jQuery 小技巧/代码片段,可以帮你快速开发. 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. $('a.top' ...
- ComboSelect 下拉筛选
Jquery Combo Select下拉筛选 http://www.dowebok.com/179.html
- vpn
https://itunes.apple.com/us/app/sonicwall-mobile-connect/id822514576?mt=12
- C#组合查询小Demo
namespace WindowsFormsApplication1 { public partial class Form1 : Form { string Sql = "select * ...
- ab post 测试 http 和 webservice 接口方法及用例
1.ab测试简单http请求 ab -n30000 -c1000 "http://10.1.1.21:8080/" 2.ab 测试 http 接口 (POST) ab -n400 ...
- goprotocbuf的安装和使用
首先得到 protobuf 相应的包文件 ,在终端上输入如下 wget http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz 下载完毕后 ...