【发布时间】: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}"和演员就可以了!谢谢!