有没有办法将命令与 WPF 工具包 DataGridHyperlinkColumn 相关联?
有什么方法可以将命令与 DataGridHyperlinkColumn 关联起来吗?我已经尝试过:
<DataGridHyperlinkColumn Header="Client Name" Binding="{Binding ShortName}">
<DataGridHyperlinkColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Hyperlink.Command"
Value="{Binding DataContext.NavigateToClientCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ClientsSummaryView}}}"/>
<Setter Property="Hyperlink.CommandParameter" Value="{Binding}"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
在运行时,我可以看到绑定正在被正确评估(调用命令的属性获取器),但是当我单击超链接时,命令没有执行。有更好的方法吗?
谢谢,
丹尼尔
Is there any way I can associate a Command with a DataGridHyperlinkColumn? I've tried this:
<DataGridHyperlinkColumn Header="Client Name" Binding="{Binding ShortName}">
<DataGridHyperlinkColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Hyperlink.Command"
Value="{Binding DataContext.NavigateToClientCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:ClientsSummaryView}}}"/>
<Setter Property="Hyperlink.CommandParameter" Value="{Binding}"/>
</Style>
</DataGridHyperlinkColumn.ElementStyle>
</DataGridHyperlinkColumn>
At runtime, I can see that the binding is being correctly evaluated (the property getter for the Command is called), but the Command is not executed when I click the hyperlink. Is there a better way to do this?
Thanks,
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定该命令与超链接关联吗?我尝试在示例应用程序中进行设置,但该命令未与超链接关联(如果从
CanExecute
返回 false,您将能够快速确定它是否已连接) 。相反,我创建了一个
DataGridTemplateColumn
来完成此任务:如果您必须创建多个列,这会变得非常烦人。使用 Reflector 打开 Toolkit 后,看起来它支持使用超链接的
TargetName
/NavigationUrl
范例。如果您在很多地方都需要这种类型的列,我建议扩展 DataGridHyperlinkColumn 并添加 Command 属性。然后,您可以修改从
GenerateElement
返回的元素,以便它使用您的命令。Are you sure the command is being associated with the hyperlink? I tried setting this up in a sample app, and the command wasn't being associated with the hyperlink (if you return false from
CanExecute
, you'll be able to quickly determine if it is wired up).Instead, I created a
DataGridTemplateColumn
to accomplish this:This would get really annoying if you had to create multiple columns. After cracking open the Toolkit with Reflector, it looks like it is supporting the
TargetName
/NavigationUrl
paradigm for using the hyperlink.If you have a scenario where you would require this type of column in many places, I would suggest extending the
DataGridHyperlinkColumn
and adding a Command property. You could then modify the element returned fromGenerateElement
so that it used your command.是的,但不适用于标准
DataGridHyperlinkColumn
。您需要稍微增强该课程。设置正确的命名空间映射后,您可以执行以下操作:
这假设您的视图模型对象上存在
NavigateCommand
。Yes, but not with the standard
DataGridHyperlinkColumn
. You need to enhance that class a little.After setting up the correct namespace mapping you can then do this:
This assumes that
NavigateCommand
exists on your viewmodel object.