【问题标题】:WPF Listbox commandsWPF 列表框命令
【发布时间】:2011-10-04 01:15:55
【问题描述】:

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

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

目前我有一个客户模型,我的虚拟机填充了列表框绑定到的客户集合。那么有没有办法处理VM上的选择更改事件,包括能够操纵列表框的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");
                    }

                }

【问题讨论】:

    标签: wpf mvvm binding listbox command


    【解决方案1】:

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

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

    这是一个使用 MVVM 灯光 (RaisePropertyChanged) 的示例。

    【讨论】:

      【解决方案2】:

      XAML:

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

      视图模型:

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

      here获取/进一步信息。

      【讨论】:

      • 我的解决方案看起来与此非常相似,我有一个问题。当用户按下取消时,我希望选择保持在原来的位置,我可以这样做,但视图会突出显示用户单击的项目,但表单其余部分上的所有帐户信息都保持不变,就像取消工作一样。
      • hmm,看来你需要在selectedindex更新后使用dispatcher返回并更改为原来的。以上代码已更新以反映这一点,更多信息here.
      • 非常感谢您提供的链接,我阅读了帖子上的 cmets 并最终使用了一种不同的方法,该方法已使用 CollectionViewSource 在其下方发布。只想提一下,如果我在绑定上将 IsAsync 属性设置为 true,则上述方法有效但似乎很慢。
      猜你喜欢
      • 1970-01-01
      • 2011-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-02
      • 2014-07-30
      • 2021-01-04
      • 2010-11-04
      相关资源
      最近更新 更多