【问题标题】:Bind buttons to different ViewModels [MVVM]将按钮绑定到不同的 ViewModel [MVVM]
【发布时间】: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();
    }
}

编辑:在一个视图中,我有 &lt;ContentPresenter Content="{Binding Content}"/&gt; 的按钮,并且内容有 ViewViewModel 我想用按钮到达

【问题讨论】:

  • 取决于视图和视图模型的连接方式
  • 在一个视图中,我有&lt;ContentPresenter Content="{Binding Content}"/&gt; 的按钮,并且内容有一个 ViewModel 我想用按钮到达的视图。
  • 您是否考虑过制作父 ViewModel 的两个 ViewModel 属性,然后通过此链接到它们?
  • 我没有,我会调查的。谢谢。

标签: c# .net wpf mvvm


【解决方案1】:

最简单和最通用的解决方案是通过在 App.xaml ResourceDictionary 中实例化您的视图模型来全局访问它们:

App.xaml:

<Application ...>
  <Application.Resources>
    <ResourceDictionary>
      <FirstViewModel x:Key="FirstViewModel" />
      <SecondViewModel x:Key="SecondViewModel" />
    </ResourceDictionary>
  </Application.Resources>
</Application>

或使用 C#(例如,App.xaml.cs)

Application.Current.Resources.Add("FirstViewModel", new FirstViewModel(arg1, arg2));
Application.Current.Resources.Add("SecondViewModel", new SecondViewModel(arg1, arg2));

现在您可以从任何视图中引用它们:

<Button Content="{Binding NameE}" 
        CommandParameter="{Binding NameE}" 
        Command="{Binding Source={StaticResource FirstViewModel}, Path=FirstCommand}" />

<Button Content="{Binding NameD}" 
        CommandParameter="{Binding NameD}" 
        Command="{Binding Source={StaticResource SecondViewModel}, Path=SecondCommand}" />

另一种解决方案是使用组合并将视图模型包装到父视图模型中:

public class MainViewModel : INotifyPropertyChanged
{
  public FirstViewModel FirstViewModel { get; private set; }
  public SecondViewModel SecondViewModel { get; private set; }

  public MainViewModel()
  {
    this.FirstViewModel = new FirstViewModel();
    this.SecondViewModel = new SecondViewModel();
  }
}

public class FirstViewModel : INotifyPropertyChanged
{
  public MyICommand<string> FirstCommand { get; private set; }

  public FirstViewModel()
  {
    FirstCommand = new MyICommand<string>(myFirstCommand);
  }

  public void myFirstCommand(string par)
  {
    Console.Beep();
  }
}


public class SecondViewModel : INotifyPropertyChanged
{
  public MyICommand<string> SecondCommand { get; private set; }

  public SecondViewModel()
  {
    SecondCommand = new MyICommand<string>(mySecondCommand);
  }

  public void mySecondCommand(string par)
  {
    Console.Beep();
  }
}

用法

<TreeView x:Name="MainTreeView">
  <TreeView.DataContext>
    <MainViewModel />
  </TreeView.DataContext>
</TreeView>


<Button Content="{Binding NameE}" 
        CommandParameter="{Binding NameE}" 
        Command="{Binding ElementName=MainTreeView, Path=DataContext.FirstViewModel.FirstCommand}" />

<Button Content="{Binding NameD}" 
        CommandParameter="{Binding NameD}" 
        Command="{Binding ElementName=MainTreeView, Path=DataContext.SecondViewModel.SecondCommand}" />

【讨论】:

  • 你能把代码放在 App.xaml 中吗?
  • 因为忘记缩进代码所以被隐藏了。
  • 这种方法的使用非常有限。通常需要多个视图模型类型的实例。并且几乎总是视图模型没有没有参数的构造函数,这是 xaml 所必需的 - 视图模型通常需要模型和一些服务
  • @ASh 当然,我知道。这就是为什么我说它是“最简单和最通用的解决方案”。没有关于他的数据上下文或应用程序结构或类详细信息的更多详细信息(无参数构造函数除外)。但是您始终可以使用 C# 将共享实例(需要构造函数参数,例如,在利用依赖注入的高级场景中)添加到应用程序资源,例如 Application.Current.Resources.Add("FirstViewModel", new FirstViewModel(arg1, arg2));
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-20
  • 2011-08-26
  • 2012-04-21
  • 2013-06-14
  • 2011-01-31
  • 2013-08-29
  • 2021-06-27
相关资源
最近更新 更多