wpf Prism 命令绑定使用参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28907112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prism Command Binding using Parameter?
提问by Ches Scehce
I have a working hyperlink as follow:
我有一个工作超链接如下:
XAML:
XAML:
<TextBlock >
<Hyperlink Command="{Binding NavHomeViewCommand}" >
<Run Text="{Binding PersonSelected.PersonKnownName}" />
</Hyperlink>
</TextBlock>
Constructor:
构造函数:
navHomeViewCommand = new DelegateCommand(NavHomeView);
Command:
命令:
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 multiple 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 have to make a new Command for each or is there a way to pass a different parameter (int) for each hyperlink to the existing NavHomeView command so I can reuse this command?
我是否必须为每个命令创建一个新命令,或者有没有办法将每个超链接的不同参数 (int) 传递给现有的 NavHomeView 命令,以便我可以重用此命令?
采纳答案by Ches Scehce
Here is a complete solution that worked for me:
这是一个对我有用的完整解决方案:
Use CommandParameter (as per Dmitry - Spasiba!)
<TextBlock> <Hyperlink CommandParameter="{Binding PersonSelected.PersonKnown2ID}" Command="{Binding NavHomeViewCommand}" > <Run Text="{Binding PersonSelected.PersonKnownName2}" /> </Hyperlink> </TextBlock>Change DelegateCommand to use object parameter
navHomeViewCommand = new DelegateCommand<object>(NavHomeView);Command Properties remain unchanged but method changed to use 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); }
使用 CommandParameter(根据 Dmitry - Spasiba!)
<TextBlock> <Hyperlink CommandParameter="{Binding PersonSelected.PersonKnown2ID}" Command="{Binding NavHomeViewCommand}" > <Run Text="{Binding PersonSelected.PersonKnownName2}" /> </Hyperlink> </TextBlock>更改 DelegateCommand 以使用对象参数
navHomeViewCommand = new DelegateCommand<object>(NavHomeView);命令属性保持不变,但方法更改为使用参数:
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); }
回答by Dzmitry Martavoi
You can use 'CommandParameter' property of the Hyperlink.
您可以使用超链接的“CommandParameter”属性。
<Hyperlink Command="{Binding NavHomeViewCommand}" CommandParameter="1" >
<Run Text="{Binding PersonSelected.PersonKnownName}" />
</Hyperlink>

