有没有办法将命令与 WPF 工具包 DataGridHyperlinkColumn 相关联? [英] Is there a way to associate a Command with a WPF Toolkit DataGridHyperlinkColumn?
问题描述
有什么方法可以将命令与 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>
在运行时,我可以看到正在正确评估绑定(调用了 Command 的属性 getter),但是当我单击超链接时没有执行 Command.有没有更好的方法来做到这一点?
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?
谢谢,
丹尼尔
推荐答案
您确定命令与超链接相关联吗?我尝试在示例应用程序中进行设置,但该命令未与超链接关联(如果您从 CanExecute
返回 false,您将能够快速确定它是否已连接).
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).
相反,我创建了一个 DataGridTemplateColumn
来完成此操作:
Instead, I created a DataGridTemplateColumn
to accomplish this:
<DataGridTemplateColumn Header="Client Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<Hyperlink Command="{Binding DataContext.NavigateToClientCommand, RelativeSource={RelativeSource AncestorType={x:Type local:ClientsSummaryView}}}"
CommandParameter="{Binding ShortName}">
<TextBlock Text="{Binding ShortName}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
如果您必须创建多个列,这会很烦人.用 Reflector 破解打开 Toolkit 后,看起来它支持使用超链接的TargetName
/NavigationUrl
范式.
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.
如果您在很多地方都需要这种类型的列,我建议扩展 DataGridHyperlinkColumn
并添加一个 Command 属性.然后您可以修改从 GenerateElement
返回的元素,以便它使用您的命令.
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 from GenerateElement
so that it used your command.
这篇关于有没有办法将命令与 WPF 工具包 DataGridHyperlinkColumn 相关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!