【发布时间】:2019-12-19 08:35:30
【问题描述】:
我在一个 View 中有两个按钮,是否可以将一个按钮绑定到第一个 ViewModel 中的一个命令,另一个按钮绑定到第二个 中的第二个命令视图模型?只需添加两个 ViewModels 都已初始化。
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.FirstCommand}" CommandParameter="{Binding NameE}">
<Label Content="{Binding NameE}"/>
</Button>
<Button Command="{Binding ElementName=MainTreeView, Path=DataContext.SecondCommand}" CommandParameter="{Binding NameD}">
<Label Content="{Binding NameD}"/>
</Button>
还有我的两个ViewModel:
public class FirstViewModel : INotifyPropertyChanged
{
public MyICommand<string> FirstCommand { get; private set; }
public HomeViewModel()
{
FirstCommand = new MyICommand<string>(myFirstCommand);
}
public void myFirstCommand(string par)
{
Console.Beep();
}
}
public class SecondViewModel : INotifyPropertyChanged
{
public MyICommand<string> SecondCommand { get; private set; }
public HomeViewModel()
{
SecondCommand = new MyICommand<string>(mySecondCommand);
}
public void mySecondCommand(string par)
{
Console.Beep();
}
}
编辑:在一个视图中,我有 <ContentPresenter Content="{Binding Content}"/> 的按钮,并且内容有 View 和 ViewModel 我想用按钮到达
【问题讨论】:
-
取决于视图和视图模型的连接方式
-
在一个视图中,我有
<ContentPresenter Content="{Binding Content}"/>的按钮,并且内容有一个 ViewModel 我想用按钮到达的视图。 -
您是否考虑过制作父 ViewModel 的两个 ViewModel 属性,然后通过此链接到它们?
-
我没有,我会调查的。谢谢。