Silverlight MVVM 和处理 FOCUS

发布于 2024-12-07 20:05:47 字数 1430 浏览 0 评论 0原文

我正在开发具有各种弹出查找等的复杂数据输入表单。由于不同的原因 - 某些控件的焦点丢失,我需要某种方法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡风轻 2024-12-14 20:05:47

它确实有气味,但我认为我们对此无能为力。

通常我对 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 single IsFocused bool somewhere in the View (since this is a View-Specific problem, and should not be mixed in with the business logic). I'll then have the View listen to some kind of Event System such as (PRISM's EventAggregator or MVVM Light's Messenger) for ResetFocus events, and I'll raise the ResetFocus event whenever something causes focus to change between my windows/pages, or after a dialog box.

It's not pretty, but it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文