WPF ComboBox SelectedItem - 更改为上一个值 [英] WPF ComboBox SelectedItem - change to previous value

查看:1227
本文介绍了WPF ComboBox SelectedItem - 更改为上一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ComboBox的SelectedItem绑定到ViewModel。

I have a ComboBox that has the SelectedItem bound to the ViewModel.

<ComboBox SelectedItem="{Binding SelItem, Mode=TwoWay}" ItemsSource="{Binding MyItems}">

当用户在View ComboBox中选择一个新项目时,我想显示一个提示,他们想要进行更改。

When the user selects a new Item in the View ComboBox, I want to display a prompt and verify that they want to make the change.

在View模型的SetItem属性设置器中,我显示一个对话框以确认选择。当他们说是的,它工作正常。

In the SetItem Property setter in the View Model, I display a Dialog to confirm the selection. When they say yes, it works fine.

我的问题是,当用户点击否我不知道谁得到ComboBox
恢复为上一个值。 ViewModel中的属性具有正确的
较早的值,但是在View中,ComboBox显示新选择的值。

My problem is, when the user clicks on "No" I am not sure who to get the ComboBox to revert back to the previous value. The Property in the ViewModel has the correct older value, however in the View the ComboBox displays the newly Selected Value.

我想让用户选择一个项目,确认他们想要继续,如果他们
决定不,我想要ComboBox恢复到上一个​​项目。

I want the user to select an item, confirm they want to go ahead with it, and if they decide not to, I want the ComboBox to revert back to the previous item.

如何我做到了吗?
谢谢!

How can I accomplish this? Thanks!

推荐答案

当用户说否时,WPF不知道该值已更改。就WPF而言,值是用户选择的任何值。

When the user says "no", WPF is unaware that the value has changed. As far as WPF is concerned, the value is whatever the user selected.

您可以尝试提高属性更改通知:

You might try raising a property changed notification:

public object SelItem
{
    get { ... }
    set
    {
        if (!CancelChange())
        {
            this.selItem = value;
        }

        OnPropertyChanged("SelItem");
    }
}

问题是,更改通知发生在同一个选择事件的上下文。因此,WPF忽略它,因为它已经知道属性已更改 - 用户选择的项目!

The problem is, the change notification happens within the same context of the selection event. Thus, WPF ignores it because it already knows the property has changed - to the item the user selected!

您需要做的是在单独的消息中引发通知事件:

What you need to do is raise the notification event in a separate message:

public object SelItem
{
    get { ... }
    set
    {
        if (CancelChange())
        {
            Dispatcher.BeginInvoke((ThreadStart)delegate
            {
                OnPropertyChanged("SelItem");
            });
            return;
        }

        this.selItem = value;
        OnPropertyChanged("SelItem");
    }
}

WPF然后会在它完成处理选择已更改的事件,因此将视图中的值恢复到它应该是什么。

WPF will then process this message after it's done processing the selection changed event and will therefore revert the value in the view back to what it should be.

您的VM显然需要访问当前 Dispatcher 。如果您需要,请参阅基本VM类的我的博文一些指示如何做到这一点。

Your VM will obviously need access to the current Dispatcher. See my blog post on a base VM class if you need some pointers on how to do this.

这篇关于WPF ComboBox SelectedItem - 更改为上一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆