Binding Commands to a Sub Viewmodel

I am trying to Bind a Command to a ViewModel which is a property of another ViewModel. If I bind to a command in the parent viewmodel (ie; the DataContext), it works fine. If I try binding to a viewmodel that's a poperty of my datacontext, it doesn't work (nothing happens). The following code illustrates how things are structured.

public class DataViewModel : ViewModelBase
{
   private ICommand _myCommand;
   public ICommand MyCommand {get{return _myCommand;}}

   public DataViewModel()
   {
       _myCommand=new DelegateCommand(myMethod,null);
   }

   private myMethod( object o)
   {
     // do whatever
   }
}

public class ParentViewModel 
{
   private DataViewModel _dataContainer=new DataViewModel();
   public DataViewModel DataContainer {get {return _dataContainer;} set {_dataContainer=value;}}
}

And my XAML looks like this: Again, my view is bound to ParentViewModel and is working fine, and commands that live directly within ParentViewModel are working fine.

<Button Command={Binding Path=DataContainer.MyCommand} Content="Get Data />

I've tried setting the DataContext of the button to the DataContainer, but that didn't work. Is this possible?

链接地址: http://www.djcxy.com/p/56164.html

上一篇: 绑定到View的基本viewmodel命令

下一篇: 将命令绑定到子视图模型