WPF列表框的命令 [英] WPF Listbox commands

查看:238
本文介绍了WPF列表框的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我概括地说程序有客户的名单。这些客户在一个列表框列出的所有所以当你点击他们所有的信息出现在表单上。这是通过数据绑定,页面上所有的控件绑定到列表框的selectedItem属性。

我想现在要做的是有询问用户是否想保存,当他们试图改变选择一个消息对话框。如果他们不这样做我想将其恢复为集合中的原始项目。如果他们打取消我想选择集中回pviously所选项目$ P $。我想知道什么是最好的办法是在MVVM的方式做到这一点?

目前我有我的客户模型和我的虚拟机填补客户的集合列表框绑定到。那么,有没有一种方法来处理在虚拟机上,将包括能够处理列表框的selectedIndex更改事件的选择?
这里是我的code所以你可以看到我在做什么。

 如果(值!= _selectedAccount)
                {
                    MessageBoxResult MBR = MessageBox.Show(是否要保存你的工作?,保存,MessageBoxButton.YesNoCancel);
                    如果(MBR == MessageBoxResult.Yes)
                    {
                        // code更新企业
                        Update_Corporate();
                        _ preSelectedAccount =价值;
                        _selectedAccount =价值;
                    }
                    如果(MBR == MessageBoxResult.No)
                    {
                        //做东西                    }
                    如果(MBR == MessageBoxResult.Cancel)
                    {                        SelectedAccount = _ preSelectedAccount;
                        NotifyPropertyChanged(SelectedAccount);
                    }                }


解决方案

XAML:

 < ListBox中的ItemsSource ={结合客户}的SelectedItem ={结合SelectedCustomer}的DisplayMemberPath =客户名称/>

视图模型:

 私人客户selectedCustomer;
公众客户SelectedCustomer
{
  得到
  {
    返回selectedCustomer;
  }
  组
  {
    如果(值!= selectedCustomer)
    {
      VAR originalValue = selectedCustomer;
      selectedCustomer =价值;
      dlgConfirm DLG =新dlgConfirm();
      VAR的结果= dlg.ShowDialog();
      如果(result.HasValue&安培;!&安培; result.Value)
      {
        Application.Current.Dispatcher.BeginInvoke(
            新的Action(()=>
            {
                selectedCustomerr = originalValue;
                OnPropertyChanged(SelectedCustomer);
            }),
            System.Windows.Threading.DispatcherPriority.ContextIdle,
            空值
        );
      }
      其他
        OnPropertyChanged(SelectedCustomer);
    }
  }
}

摘自这里

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");
                    }

                }

解决方案

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.

这篇关于WPF列表框的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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