我需要强制Silverlight提交聚焦元素的代码(在我的例子中是TextBox,但它可以是任何东西)。在WPF中我使用
public static void CommitFocusedElement() {
UIElement element = Keyboard.FocusedElement as UIElement;
if (element != null) {
TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
FocusNavigationDirection directionBack = FocusNavigationDirection.Previous;
if (!element.MoveFocus(request)) {
request = new TraversalRequest(FocusNavigationDirection.Previous);
directionBack = FocusNavigationDirection.Next;
element.MoveFocus(request);
}
if (element.Focusable)
{
element.Focus();
}
else
{
element.MoveFocus(new TraversalRequest(directionBack));
}
}
}
但此代码的几个部分与Silverlight不兼容。有人能指出我的Silverlight替代品吗?
答案 0 :(得分:1)
我假设您要更新绑定的来源。如果你不是,你可能应该这样做。
BindingExpression expression = textBox1.GetBindingExpression(TextBox.TextProperty);
expression.UpdateSource();