Prism command binding using?

I have a working hyperlink:

XAML:

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

Constructor:

 navHomeViewCommand = new DelegateCommand(NavHomeView);

Team:

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

If I want to have some hyperlinks, such as ...

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

Do I need to create a new command for each or is there a way to pass a different parameter (int) for each hyperlink to an existing NavHomeView command so that I can reuse this command?

+6
source share
2 answers

Here is the complete solution that worked for me:

  1. Use CommandParameter (according to Dmitry - Thank you!)

    <TextBlock>
        <Hyperlink CommandParameter="{Binding PersonSelected.PersonKnown2ID}"
                   Command="{Binding NavHomeViewCommand}" >
            <Run Text="{Binding PersonSelected.PersonKnownName2}" />
        </Hyperlink>
    </TextBlock>
    
  2. Modify DelegateCommand to use an object parameter

    navHomeViewCommand = new DelegateCommand<object>(NavHomeView);
    
  3. The properties of the command remain unchanged, but the method is changed to use the parameter:

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

CommandParameter .

 <Hyperlink Command="{Binding NavHomeViewCommand}" CommandParameter="1" >
       <Run Text="{Binding PersonSelected.PersonKnownName}" />
 </Hyperlink>
+2

All Articles