MVVM 视觉状态管理器和焦点

发布于 2024-08-30 08:38:13 字数 167 浏览 6 评论 0原文

使用 Silverlight 4。

我有两种视觉状态可供控制。我想在状态发生变化时将焦点从一个文本框更改为另一个文本框。

使用 MVVM 执行此操作的最佳方法是什么?

我希望使用 VisualStateManager 来做到这一点或一种行为......但我还没有找到办法。

Using Silverlight 4.

I have two visual states for my control. I want to change the focus from one textbox to another when the states change.

What is the best way to do this using MVVM?

I was hoping to use the visualstatemanager to do it or a behavior... but I have not figured out a way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小忆控 2024-09-06 08:38:13

如果我是您,我会创建一个具有 FocusBehavior.IsFocused 属性的 FocusBehaviour,在您的控件上添加该行为,并在 VSM 状态中设置 IsFocused=True。

If I were you I'd create a FocusBehaviour, with a FocusBehavior.IsFocused property, add that Behaviour on your Control and in the VSM state set IsFocused=True.

一个人的旅程 2024-09-06 08:38:13

更改文本框之间的焦点绝对是特定于视图的代码,因此我认为它可能应该在视图后面的代码中完成。有些人建议根本没有代码,但我认为这有点夸张。

至于如何从 ViewModel 触发它,我会这样做:

class MyView : UserControl {

    // gets or sets the viewmodel attached to the view
    public MyViewModel ViewModel {
        get {...}
        set {
           // ... whatever method you're using for attaching the
           // viewmodel to a view
           myViewModel = value;
           myViewModel.PropertyChanged += ViewModel_PropertyChanged;
    }

    private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) {
        if (e.PropertyName == "State") {
            VisualStateManager.GoToState(this, ViewModel.State, true);
            if (ViewModel.State == "FirstState") {
                textBox1.Focus();
            }
            else if (ViewModel.State == "SecondState") {
                textBox2.Focus();
            }
        }
    }

}

class MyViewModel : INotifyPropertyChanged {

    // gets the current state of the viewmodel
    public string State {
        get { ... }
        private set { ... } // with PropertyChanged event
    }

    // replace this method with whatever triggers your
    // state change, such as a command handler
    public void ToggleState() {
        if (State == "SecondState") { State = "FirstState"; }
        else { State = "SecondState"; }
    }

}

Changing the focus between text boxes is most definitely view-specific code so I think it should probably be done in the code behind of the view. Some people suggest having no code at all but I think that's a bit of an exaggeration.

As for how to trigger it from the ViewModel, I would do something like:

class MyView : UserControl {

    // gets or sets the viewmodel attached to the view
    public MyViewModel ViewModel {
        get {...}
        set {
           // ... whatever method you're using for attaching the
           // viewmodel to a view
           myViewModel = value;
           myViewModel.PropertyChanged += ViewModel_PropertyChanged;
    }

    private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) {
        if (e.PropertyName == "State") {
            VisualStateManager.GoToState(this, ViewModel.State, true);
            if (ViewModel.State == "FirstState") {
                textBox1.Focus();
            }
            else if (ViewModel.State == "SecondState") {
                textBox2.Focus();
            }
        }
    }

}

class MyViewModel : INotifyPropertyChanged {

    // gets the current state of the viewmodel
    public string State {
        get { ... }
        private set { ... } // with PropertyChanged event
    }

    // replace this method with whatever triggers your
    // state change, such as a command handler
    public void ToggleState() {
        if (State == "SecondState") { State = "FirstState"; }
        else { State = "SecondState"; }
    }

}
静若繁花 2024-09-06 08:38:13

The solution from the C#er blog is pretty similar to JustinAngle's answer but I figured since it is a Silverlight specific solution it bears mentioning. Basically Jeremy Likeness creates a dummy control that he calls FocusHelper that behaves very much like FocusBehavior.

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