WPF 列表框命令

发布于 2024-11-19 17:58:18 字数 1250 浏览 5 评论 0原文

好吧,简而言之,我的程序有一个客户列表。这些客户都列在一个列表框中,因此当单击其中一个时,他们的所有信息都会显示在表单上。这是通过数据绑定实现的,页面上的所有控件都绑定到列表框的 selectedItem。

我现在想做的是有一个消息对话框,询问用户在尝试更改选择时是否要保存。如果他们不这样做,我想将其恢复为集合中的原始项目。如果他们点击取消,我希望选择重新集中在之前选择的项目上。我想知道以 MVVM 方式完成此任务的最佳方法是什么?

目前,我有一个客户模型,并且我的虚拟机填充了列表框绑定到的客户集合。那么有没有一种方法可以处理虚拟机上的选择更改事件,其中包括能够操作列表框的 selectedIndex ? 这是我的代码,这样你就可以看到我在做什么。

                if (value != _selectedAccount)
                {
                    MessageBoxResult mbr = MessageBox.Show("Do you want to save your work?", "Save", MessageBoxButton.YesNoCancel);
                    if (mbr == MessageBoxResult.Yes)
                    {
                        //Code to update corporate
                        Update_Corporate();
                        _preSelectedAccount = value;
                        _selectedAccount = value;
                    }
                    if (mbr == MessageBoxResult.No)
                    {
                        //Do Stuff

                    }
                    if (mbr == MessageBoxResult.Cancel)
                    {

                        SelectedAccount = _preSelectedAccount;
                        NotifyPropertyChanged("SelectedAccount");
                    }

                }

Ok, my program in a nutshell has a list of customers. Those customers are all listed in a listbox so when one is clicked on all of their information appears on the form. This works through databinding, all of the controls on the page are bound to the listbox's selectedItem.

What I would like to do now is have a message dialog that asks if the user would like to save when they try to change the selection. If they don't I want to revert it back to the original item in the collection. If they hit cancel I want the selection to focus back on the previously selected item. I am wondering what the best way would be to accomplish this in an MVVM manner?

Currently I have a Model for my customer and my VM fills a collection of Customers that the listbox is bound to. So is there a way to handle the selection changed event on the VM that would include being able to manipulate the selectedIndex of the listbox?
Here is my code so you can see what I am doing.

                if (value != _selectedAccount)
                {
                    MessageBoxResult mbr = MessageBox.Show("Do you want to save your work?", "Save", MessageBoxButton.YesNoCancel);
                    if (mbr == MessageBoxResult.Yes)
                    {
                        //Code to update corporate
                        Update_Corporate();
                        _preSelectedAccount = value;
                        _selectedAccount = value;
                    }
                    if (mbr == MessageBoxResult.No)
                    {
                        //Do Stuff

                    }
                    if (mbr == MessageBoxResult.Cancel)
                    {

                        SelectedAccount = _preSelectedAccount;
                        NotifyPropertyChanged("SelectedAccount");
                    }

                }

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

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

发布评论

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

评论(2

只涨不跌 2024-11-26 17:58:18

捕获更改事件的最佳方法是将列表框的 SelectedItem 绑定到视图模型中的另一个属性,然后在集合上您可以执行您需要执行的操作:

private Customer selectedCustomer;
public Customer SelectedCustomer
{
    get { return selectedCustomer; }
    set
    {
        if (selectedCustomer== value) return;
        selectedCustomer = value;
        RaisePropertyChanged("SelectedCustomer");
        // Do your stuff here
    }
}

这是使用 MVVM light (RaisePropertyChanged) 的示例。

the best way you can catch the changed event is to bind the SelectedItem of the listbox to another property in your view model, then on the set you can do what you need to do:

private Customer selectedCustomer;
public Customer SelectedCustomer
{
    get { return selectedCustomer; }
    set
    {
        if (selectedCustomer== value) return;
        selectedCustomer = value;
        RaisePropertyChanged("SelectedCustomer");
        // Do your stuff here
    }
}

This is an example using MVVM light (RaisePropertyChanged).

始终不够 2024-11-26 17:58:18

XAML:

<ListBox ItemsSource="{Binding Customers}" SelectedItem="{Binding SelectedCustomer}" DisplayMemberPath="CustomerName"/>

ViewModel:

private Customer selectedCustomer;
public Customer SelectedCustomer
{
  get
  {
    return selectedCustomer;
  }
  set
  {
    if (value != selectedCustomer)
    {
      var originalValue = selectedCustomer;
      selectedCustomer = value;
      dlgConfirm dlg = new dlgConfirm();
      var result = dlg.ShowDialog();
      if (!result.HasValue && result.Value)
      {
        Application.Current.Dispatcher.BeginInvoke(
            new Action(() =>
            {
                selectedCustomerr = originalValue;
                OnPropertyChanged("SelectedCustomer");
            }),
            System.Windows.Threading.DispatcherPriority.ContextIdle,
            null
        );
      }
      else
        OnPropertyChanged("SelectedCustomer");
    }
  }
}

此处

XAML:

<ListBox ItemsSource="{Binding Customers}" SelectedItem="{Binding SelectedCustomer}" DisplayMemberPath="CustomerName"/>

ViewModel:

private Customer selectedCustomer;
public Customer SelectedCustomer
{
  get
  {
    return selectedCustomer;
  }
  set
  {
    if (value != selectedCustomer)
    {
      var originalValue = selectedCustomer;
      selectedCustomer = value;
      dlgConfirm dlg = new dlgConfirm();
      var result = dlg.ShowDialog();
      if (!result.HasValue && result.Value)
      {
        Application.Current.Dispatcher.BeginInvoke(
            new Action(() =>
            {
                selectedCustomerr = originalValue;
                OnPropertyChanged("SelectedCustomer");
            }),
            System.Windows.Threading.DispatcherPriority.ContextIdle,
            null
        );
      }
      else
        OnPropertyChanged("SelectedCustomer");
    }
  }
}

Taken/further info from here.

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