c# - 使用参数的 Prism 命令绑定(bind)?

标签 c# wpf command parameter-passing prism

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

XAML:

 <TextBlock >
    <Hyperlink Command="{Binding NavHomeViewCommand}" >
       <Run Text="{Binding PersonSelected.PersonKnownName}" />
    </Hyperlink>
</TextBlock>

构造函数:

 navHomeViewCommand = new DelegateCommand(NavHomeView);

命令:

     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);
    }

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

     <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>

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

最佳答案

这是一个对我有用的完整解决方案:

  1. 使用 CommandParameter(根据 Dmitry - Spasiba!)

    <TextBlock>
        <Hyperlink CommandParameter="{Binding PersonSelected.PersonKnown2ID}"
                   Command="{Binding NavHomeViewCommand}" >
            <Run Text="{Binding PersonSelected.PersonKnownName2}" />
        </Hyperlink>
    </TextBlock>
    
  2. 更改 DelegateCommand 以使用对象参数

    navHomeViewCommand = new DelegateCommand<object>(NavHomeView);
    
  3. 命令属性保持不变,但方法更改为使用参数:

    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);
    }
    

关于c# - 使用参数的 Prism 命令绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28907112/

相关文章:

c# - 自定义调用 Action 的 ASP Web API Json 序列化

c# - 如何在带有自定义 ContentTemplate 的 WPF 按钮上使用 accesskey?

wpf - 将 WPF ContextMenu MenuItem 绑定(bind)到 UserControl 属性与 ViewModel 属性

shell - 从 UEFI 应用程序内部运行 UEFI shell 命令

c# - 用于 64 位 C# 开发的 CodeSite 替代品

c# - WCF 4 - Soap 和 REST 端点

c# - ASP .NET Core IIS 托管用户身份名称为空且 IsAuthenticated=false

c# - 在 WPF 应用程序中应用 MVVM 模式

wpf - 无法访问 app.xaml 中定义的资源

c - 从命令提示符执行程序