如何通过CommandParameter传递对象的引用

时间:2016-05-13 01:03:35

标签: c# wpf xaml mvvm

我有一个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函数,因此无法实现我所需要的。

无论如何通过引用传递供应商?

1 个答案:

答案 0 :(得分:-1)

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

相关问题