多个按钮的一个视图模型

xan*_*key 1 c# wpf mvvm

我是WPF和MVVM的新手.我正在尝试理清如何拥有一个干净的项目结构,其中有多个按钮可以启动不同的应用程序(如chrome,IE,记事本).是否可以为多个按钮设置一个ViewModel?

我已经开始使用这里的代码,我尝试解决方案是使该链接的代码成为视图模型库,并为扩展所述基础的每个按钮提供其他视图模型.

每个人都有一个独特的行动:

    public override void ClickAction()
    {
        Process ieProcess = new Process();
        ieProcess.StartInfo.FileName = "IExplore.exe";
        ieProcess.Start();
    }
Run Code Online (Sandbox Code Playgroud)

但是,我不确定这样做的正确方法.如何使用此方法创建DataContext?任何帮助表示赞赏.

Bra*_*NET 5

每个视图应该有一个视图模型; 不是视图中的每个项目(在这种情况下,按钮).执行您所描述的操作的简单方法是使用CommmandParameter属性传递您的exe名称:

<Button Content="Launch IE" Command="{Binding LaunchAppCommand}" CommandParameter="IExplorer.exe"/>
<Button Content="Launch Notepad" Command="{Binding LaunchAppCommand}" CommandParameter="notepad.exe"/>
<Button Content="Launch Chrome" Command="{Binding LaunchAppCommand}" CommandParameter="chrome.exe"/>
Run Code Online (Sandbox Code Playgroud)

用你的命令:

public ICommand LaunchAppComand {get; private set;}
...
public MyViewModel()
{
    LaunchAppCommand = new DelegateCommand(LaunchApp);
}
...
private void LaunchApp(object parameter)
{
     string processName = (string)parameter;
     Process launchProc = new Process();
     launchProc.StartInfo.FileName = processName;
     launchProc.Start();
}
Run Code Online (Sandbox Code Playgroud)

为了避免对所有按钮进行硬编码,可以使用a ItemsControl,它会为它创建的每个模板设置单独的数据上下文.为此,您需要一组数据类,以及一种稍微不同的方式来获取命令:

//ProcessShortcut.cs
public class ProcessShortcut
{
    public string DisplayName {get; set;}
    public string ProcessName {get; set;}
}

//MyViewModel.cs, include the previous code

//INotifyPropertyChanged left out for brevity
public IEnumerable<ProcessShortcut> Shortcuts {get; set;} 

public MyViewModel()
{
    Shortcuts = new List<ProcessShortcut>()
    {
        new ProcessShortcut(){DisplayName = "IE", ProcessName="IExplorer.exe"},
        new ProcessShortcut(){DisplayName = "Notepad", ProcessName="notepad.exe"},
        new ProcessShortcut(){DisplayName = "Chrome", ProcessName="chrome.exe"},
    };
}

//MyView.xaml
<Window x:Name="Root">
...
   <ItemsControl ItemsSource="{Binding Shortcuts}">
      <ItemsControl.ItemTemplate>
          <DataTemplate>
              <Button Content="{Binding DisplayName, StringFormat='{}Start {0}'}" 
                      Command="{Binding ElementName=Root, Path=DataContext.LaunchAppCommand}" 
                      CommandParameter="{Binding ProcessName}"/>
          </DataTemplate>
      </ItemsControl.ItemTemplate>
   </ItemsControl>
...
</Window>
Run Code Online (Sandbox Code Playgroud)

因为将模板内部ItemsControl设置为DataContext绑定项,所以需要ElementName绑定才能获取命令,并且不需要限定对成员的访问权限ProcessShortcut.从长远来看,这是你通常想要的方法,当你有这样的重复控件.