UpdateSourceTrigger in Silverlight Binding

Goldfire_001 2010-01-11 12:09:11
Silverlight 的DataBinding中,UpdateSourceTrigger枚举只有两个:Default和Explicit。

在Silverlight中,没有了WPF中LostFocus和PropertyChanged的UpdateSourceTrigger的控制,貌似官方的意思是,所有的Binding默认都是OneWay,并且是PropertyChanged。

翻了一下Code,发现微软很猥琐的把TextBox.TextProperty和PasswordBox.PasswordProperty给Hard Code成LostFocus。

问题是:那么,如果我自己注册一个DP。如何才能把它的default UpdateSourceTrigger实现成LostFocus(或者可控制)呢?

等高手的思路。
...全文
167 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Goldfire_001 2010-01-13
  • 打赏
  • 举报
回复
参见了很多第三方控件的方法,最终还是把TextBox给包了。

不过,还是非常谢谢你,也是一种很好的思路。
jv9 2010-01-13
  • 打赏
  • 举报
回复
在Silverlight中,没有了WPF中LostFocus和PropertyChanged的UpdateSourceTrigger的控制,貌似官方的意思是,所有的Binding默认都是OneWay,并且是PropertyChanged。

这里你理解的有问题。

可以这样使用,具体的UpdateSourceTriggerHelper可以自己来写。例如:
<TextBox Text="{Binding FirstName, Mode=TwoWay}local:BindingHelper.UpdateSourceOnChange="True" />

根据你的问题可以这样:


private static void OnUpdateSourceTriggerChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
TextBox textBox = d as TextBox;
if ((bool)e.OldValue)
{
textBox.TextChanged -= (s, arg) => {

};
}
if ((bool)e.NewValue)
{
textBox.TextChanged += (s, arg) => {
var c = findFocusableControl(textBox);
if (c != null)
{
c.Focus();
}
textBox.Focus();
};
}
}


private static Control findFocusableControl(Control control)
{
var ctl = VisualTreeHelper.GetParent(control);
if ((ctl as Control) != null)
{
return ctl as Control;
}
else
{
int childrenCount = VisualTreeHelper.GetChildrenCount(ctl);
for (int i = 0; i < childrenCount; i++)
{
var c = VisualTreeHelper.GetChild(ctl, i) as Control;
if ((c != null) &amp;amp;amp;amp;amp;amp;&amp;amp;amp;amp;amp;amp; (c != control))
{
return c;
}
}
}
return null;
}
}



调用很简单,


<TextBox x:Name="nameTextbox" Height="25" Width="100" Margin="5" Text="{Binding Name, Mode=TwoWay}"
local:UpdateSourceTriggerHelper.UpdateSourceTrigger="True" />


详细可以参看:
http://silverlightchina.net/html/developer/2010/0112/572.html
http://silverlightchina.net/html/developer/2010/0112/573.html
Goldfire_001 2010-01-12
  • 打赏
  • 举报
回复
不会吧。我叙述的不详细?那我自己先顶吧。

8,734

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧