如何将命令绑定到它上面的 class?
How do I bind a command to a class above it?
大家好。
首先,我正在使用 MVVM 模式,但遇到以下问题。
我有一个 Main UserControl,其中有一个 ContentControl 和一个按钮。
此按钮更改 ContentControl“LoadSecondVMCommand”的内容。
在随后加载的 UserControl(第二个 UserControl)中有一个后退按钮,然后应该访问命令 LoadFirstVMCommand,
再次加载 FirstViewModel。所以我必须能够以某种方式从第二个用户控件访问 MainUserControl class 中的 LoadFirstVMCommand。
我如何进行绑定或如何解决它,我可以从第二个 UserControl 更改 ContentControlView。
//Main UserControl
<UserControl>
<Grid>
<Button Command="{Binding LoadSecondVMVMCommand}">Load</Button>
<ContentControl Content="{Binding ContentControlView}"/>
</Grid>
</UserControl>
namespace ViewModels
{
public class MainUserControl : ObservableObject
{
private object _contentControlView;
public object ContentControlView
{
get { return _devicesView; }
set { OnPropertyChanged(ref _devicesView, value); }
}
public ICommand LoadFirstVMCommand { get; private set; }
public ICommand LoadSecondVMCommand { get; private set; }
public DeviceViewModel()
{
LoadFirstVMCommand = new RelayCommand(LoadFirstVM);
LoadSecondVMCommand = new RelayCommand(LoadSecondVM);
ContentControlView = new FirstViewModel();
}
private void LoadFirstVM()
{
ContentControlView = new FirstViewModel();
}
private void LoadSecondVM()
{
ContentControlView = new SecondViewModel();
}
}
}
//Second UserControl
<UserControl>
<Grid>
<Button>Back</Button> //this button should access the Command LoadFirstVMCommand in class MainUserControl to LoadFirstVM
</Grid>
</UserControl>
private void LoadSecondVM()
{
ContentControlView = new SecondViewModel(LoadFirstVMCommand);
}
...
SecondViewModel 的构造函数:
public SecondViewModel(ICommand backCommand)
{
this.BackCommand = backCommand;
}
大家好。
首先,我正在使用 MVVM 模式,但遇到以下问题。 我有一个 Main UserControl,其中有一个 ContentControl 和一个按钮。 此按钮更改 ContentControl“LoadSecondVMCommand”的内容。 在随后加载的 UserControl(第二个 UserControl)中有一个后退按钮,然后应该访问命令 LoadFirstVMCommand, 再次加载 FirstViewModel。所以我必须能够以某种方式从第二个用户控件访问 MainUserControl class 中的 LoadFirstVMCommand。 我如何进行绑定或如何解决它,我可以从第二个 UserControl 更改 ContentControlView。
//Main UserControl
<UserControl>
<Grid>
<Button Command="{Binding LoadSecondVMVMCommand}">Load</Button>
<ContentControl Content="{Binding ContentControlView}"/>
</Grid>
</UserControl>
namespace ViewModels
{
public class MainUserControl : ObservableObject
{
private object _contentControlView;
public object ContentControlView
{
get { return _devicesView; }
set { OnPropertyChanged(ref _devicesView, value); }
}
public ICommand LoadFirstVMCommand { get; private set; }
public ICommand LoadSecondVMCommand { get; private set; }
public DeviceViewModel()
{
LoadFirstVMCommand = new RelayCommand(LoadFirstVM);
LoadSecondVMCommand = new RelayCommand(LoadSecondVM);
ContentControlView = new FirstViewModel();
}
private void LoadFirstVM()
{
ContentControlView = new FirstViewModel();
}
private void LoadSecondVM()
{
ContentControlView = new SecondViewModel();
}
}
}
//Second UserControl
<UserControl>
<Grid>
<Button>Back</Button> //this button should access the Command LoadFirstVMCommand in class MainUserControl to LoadFirstVM
</Grid>
</UserControl>
private void LoadSecondVM()
{
ContentControlView = new SecondViewModel(LoadFirstVMCommand);
}
...
SecondViewModel 的构造函数:
public SecondViewModel(ICommand backCommand)
{
this.BackCommand = backCommand;
}