简体   繁体   中英

How to pass a reference to an object through CommandParameter

I have a MainViewModel class which is the basis for my navigation.

Within that class I have this method with the purpose of changing the parameter object passed in to the selected vendor.

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

The class that is affected by this method is below:

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

            ........
    }

I am essentially trying to pass the Vendor property as a reference type through the CommandParameter property to the ShowVendorDialog which is executed through a RelayCommand, I am just not sure how to accomplish the reference part.

Here is the xaml that binds to the ShowVendorDialogCommand.

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

This does not accomplish what I need as the Vendor property is passed by value into the ShowVendorDialog function.

Is there anyway to pass the Vendor by reference?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM