使用参数进行Prism命令绑定?

Che*_*hce 7 c# wpf command prism parameter-passing

我有一个工作超链接如下:

XAML:

 <TextBlock >
    <Hyperlink Command="{Binding NavHomeViewCommand}" >
       <Run Text="{Binding PersonSelected.PersonKnownName}" />
    </Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

构造函数:

 navHomeViewCommand = new DelegateCommand(NavHomeView);
Run Code Online (Sandbox Code Playgroud)

命令:

     private readonly ICommand navHomeViewCommand;
    public ICommand NavHomeViewCommand
    {
        get
        { return navHomeViewCommand; }
    }
    private void NavHomeView()
    {
        int val;
        val = PersonSelected.PersonKnownID);
        var parameters = new NavigationParameters();
        parameters.Add("To", val);
        _regionManager.RequestNavigate("MainRegion", new Uri("HomeView", UriKind.Relative), parameters);
    }
Run Code Online (Sandbox Code Playgroud)

如果我想有多个超链接,如...

     <Hyperlink Command="{Binding NavHomeViewCommand}" >
       <Run Text="{Binding PersonSelected.PersonKnownName}" />
    </Hyperlink>
    <Hyperlink Command="{Binding NavHomeViewCommand}" >
       <Run Text="{Binding PersonSelected.PersonKnownName2}" />
    </Hyperlink>
    <Hyperlink Command="{Binding NavHomeViewCommand}" >
       <Run Text="{Binding PersonSelected.PersonKnownName3}" />
    </Hyperlink>
Run Code Online (Sandbox Code Playgroud)

我是否必须为每个命令创建一个新命令,或者是否有办法为现有NavHomeView命令的每个超链接传递一个不同的参数(int),以便我可以重用此命令?

Che*_*hce 7

这是一个适合我的完整解决方案:

  1. 使用CommandParameter(根据Dmitry - Spasiba!)
<TextBlock>
    <Hyperlink CommandParameter="{Binding PersonSelected.PersonKnown2ID}"
               Command="{Binding NavHomeViewCommand}" >
        <Run Text="{Binding PersonSelected.PersonKnownName2}" />
    </Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
  1. 将DelegateCommand更改为使用object参数
navHomeViewCommand = new DelegateCommand<object>(NavHomeView);
Run Code Online (Sandbox Code Playgroud)
  1. 命令属性保持不变但方法已更改为使用参数:
private readonly ICommand navHomeViewCommand;
public ICommand NavHomeViewCommand
{
    get { return navHomeViewCommand; }
}

private void NavHomeView(object ID)
{
    int val = Convert.ToInt32(ID);
    var parameters = new NavigationParameters();
    parameters.Add("To", val);
   _regionManager.RequestNavigate("MainRegion", new Uri("HomeView", UriKind.Relative), parameters);
}
Run Code Online (Sandbox Code Playgroud)