【问题标题】:How to pass a reference to an object through CommandParameter如何通过 CommandParameter 传递对对象的引用
【发布时间】:2016-05-13 01:03:34
【问题描述】:

我有一个 MainViewModel 类,它是我导航的基础。

在该类中,我有这个方法,目的是更改传递给所选供应商的参数对象。

class MainViewModel
{
        public Command ShowVendorDialogCommand
        {
            get;
            private set;
        }
        private void ShowVendorDialog(object parameter)
        {
            if (parameter != null)
            {
                VendorDialog vd = new VendorDialog();
                VendorDialogViewModel vm = new VendorDialogViewModel();
                vd.DataContext = vm;
                vm.PropertyChanged += (s, e) =>
                {
                    if (e.PropertyName == "CloseDialog")
                    {
                        vd.Close();
                    }
                };
                vd.ShowDialog();

                if (vm.DialogResult)
                {
                    parameter = vm.SelectedVendor.Copy() as Vendor;
                }
            }
        }
}

受此方法影响的类如下:

    class InventoryStyleSingleViewModel
    {
            public Vendor
            { 
                 get
                 {
                      return _Vendor;
                 }
                 set
                 {
                      if (value != null)
                      {
                           _Vendor = value;
                           OnPropertyChanged("Vendor");
                      }
                 }
            }
            private Vendor _Vendor;

            ........
    }

我实际上是在尝试通过 CommandParameter 属性将 Vendor 属性作为引用类型传递给通过 RelayCommand 执行的 ShowVendorDialog,我只是不确定如何完成引用部分。

这是绑定到 ShowVendorDialogCommand 的 xaml。

<Button Width="50" DockPanel.Dock="Left" Command="{Binding ElementName=BeginWindow, Path=DataContext.ShowVendorDialogCommand}" CommandParameter="{Binding Vendor}" Content="..." />

这并不能满足我的需要,因为 Vendor 属性是按值传递给 ShowVendorDialog 函数的。

有没有办法通过引用传递Vendor?

【问题讨论】:

  • 您是否尝试与上下文一起指定?喜欢DataConext.Vendor?
  • 成功了! Command="{Binding}" 和演员就可以了!谢谢!

标签: c# wpf xaml mvvm


【解决方案1】:

private void ShowVendorDialog(ref object parameter) 不起作用吗?

【讨论】:

  • 不适用于我的 Action 在我的 Command 类中的定义方式。我将不得不改变更多的东西。
猜你喜欢
  • 1970-01-01
  • 2017-09-11
  • 2012-02-03
  • 2019-06-05
  • 2011-07-06
  • 2012-07-07
  • 2012-01-05
  • 2012-07-16
相关资源
最近更新 更多