Silverlight MVVM 和处理 FOCUS
我正在开发具有各种弹出查找等的复杂数据输入表单。由于不同的原因 - 某些控件的焦点丢失,我需要某种方法在 MVVM 中设置焦点。到目前为止,我想出了附加属性,我像这样编码(跳过了实际的依赖属性声明):
private static void SetFocus(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var textBox = d as TextBox;
if (textBox != null)
{
textBox.Focus();
}
}
所以,它非常简单。当属性改变时 - 焦点被设置。
我的观点:
<TextBox Text="{Binding CurrentItem.SerialNumber, Mode=TwoWay, NotifyOnValidationError=True}"
behaviors:TextBoxBehaviors.IsFocused="{Binding SecondaryControlFocus}"
Grid.Column="1" Grid.Row="2" Margin="1" Grid.ColumnSpan="2" TabIndex="2" />
如您所见 - 我附加该行为并绑定到“SecondaryControlFocus”属性。
ViewModel:
public bool SecondaryControlFocus
{
get
{
return this.secondaryControlFocus;
}
set
{
this.secondaryControlFocus = value;
this.RaisePropertyChanged(() => this.SecondaryControlFocus);
}
}
以及如何设置焦点的代码:
this.SecondaryControlFocus = !this.SecondaryControlFocus;
对我来说,这段代码闻起来,因为我必须切换属性力并返回才能触发属性。
有没有更好的方法来完成我所做的事情?当高级用户无法使用 TAB 键时,没有什么比这更令人恼火的了...而且我需要控制 MVVM 中的焦点,这对于正确的数据输入流程很重要。但我希望代码有点“好”
I'm developing complex data entry forms with various pop-up lookups, etc. Because of different things - focus of certain controls get lost and I need some way to set focus in MVVM. So far I came up with attached property which I coded like this(actual dependency property declaration skipped):
private static void SetFocus(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var textBox = d as TextBox;
if (textBox != null)
{
textBox.Focus();
}
}
So, it's pretty simple. When property changes - focus get's set.
My view:
<TextBox Text="{Binding CurrentItem.SerialNumber, Mode=TwoWay, NotifyOnValidationError=True}"
behaviors:TextBoxBehaviors.IsFocused="{Binding SecondaryControlFocus}"
Grid.Column="1" Grid.Row="2" Margin="1" Grid.ColumnSpan="2" TabIndex="2" />
As you see - I attach that behavior and Bind to "SecondaryControlFocus" property.
ViewModel:
public bool SecondaryControlFocus
{
get
{
return this.secondaryControlFocus;
}
set
{
this.secondaryControlFocus = value;
this.RaisePropertyChanged(() => this.SecondaryControlFocus);
}
}
And code how I set focus:
this.SecondaryControlFocus = !this.SecondaryControlFocus;
To me this code smells because I have to toggle property force and back in order to trigger property..
Is there nicer way to accomplish what I do? There is nothing more irritating when power user can't use TAB keys... And I need to get control over focusing in MVVM, this is important for proper data entry flow. But I want code to be somewhat "nice"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它确实有气味,但我认为我们对此无能为力。
通常我对
AttachedProperty
执行相同的操作,并在某处保留一个IsFocused
bool在View
中(因为这是一个特定于视图的问题,不应与业务逻辑混合)。然后,我将让View
监听某种事件系统,例如(PRISM 的EventAggregator
或 MVVM Light 的Messenger
)以进行ResetFocus
事件,每当某些情况导致焦点在我的窗口/页面之间或在对话框之后发生变化时,我都会引发ResetFocus
事件。它不漂亮,但很有效。
It does smell, but I don't think there's anything we can do about it
Usually I do the same thing you have with the
AttachedProperty
, and keep a singleIsFocused
bool somewhere in theView
(since this is a View-Specific problem, and should not be mixed in with the business logic). I'll then have theView
listen to some kind of Event System such as (PRISM'sEventAggregator
or MVVM Light'sMessenger
) forResetFocus
events, and I'll raise theResetFocus
event whenever something causes focus to change between my windows/pages, or after a dialog box.It's not pretty, but it works.