有没有办法将一个命令与一个WPF工具包DataGridHyperlinkColumn? [英] Is there a way to associate a Command with a WPF Toolkit DataGridHyperlinkColumn?
问题描述
有什么办法我可以关联一个命令与DataGridHyperlink列吗?我尝试过:
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>
在运行时,我可以看到绑定正在被正确求值(命令的属性getter调用),但是当我单击超链接时不执行命令。有更好的方法吗?
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>
如果你必须创建多个列,这将会非常讨厌。破解后使用反射器打开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屋!