有没有办法将命令与 WPF 工具包 DataGridHyperlinkColumn 相关联?

发布于 2024-08-29 07:55:28 字数 831 浏览 7 评论 0原文

有什么方法可以将命令与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾兮 2024-09-05 07:55:28

您确定该命令与超链接关联吗?我尝试在示例应用程序中进行设置,但该命令未与超链接关联(如果从 CanExecute 返回 false,您将能够快速确定它是否已连接) 。

相反,我创建了一个 DataGridTemplateColumn 来完成此任务:

<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 范例。

如果您在很多地方都需要这种类型的列,我建议扩展 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:

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

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 from GenerateElement so that it used your command.

夢归不見 2024-09-05 07:55:28

是的,但不适用于标准 DataGridHyperlinkColumn。您需要稍微增强该课程。

public class DataGridHyperlinkColumn : System.Windows.Controls.DataGridHyperlinkColumn
{
    /// <summary>
    /// Support binding the hyperlink to an ICommand rather than a Uri
    /// </summary>
    public BindingBase CommandBinding { get; set; }

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        var result = base.GenerateElement(cell, dataItem);

        if (((TextBlock)result).Inlines.FirstInline is Hyperlink link)
            BindingOperations.SetBinding(link, Hyperlink.CommandProperty, CommandBinding);

        return result;
    }
}

设置正确的命名空间映射后,您可以执行以下操作:

<c:DataGridHyperlinkColumn Header="Booking" Binding="{Binding Path=ReservationNo}" CommandBinding="{Binding Path=NavigateCommand}" />

这假设您的视图模型对象上存在 NavigateCommand

Yes, but not with the standard DataGridHyperlinkColumn. You need to enhance that class a little.

public class DataGridHyperlinkColumn : System.Windows.Controls.DataGridHyperlinkColumn
{
    /// <summary>
    /// Support binding the hyperlink to an ICommand rather than a Uri
    /// </summary>
    public BindingBase CommandBinding { get; set; }

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        var result = base.GenerateElement(cell, dataItem);

        if (((TextBlock)result).Inlines.FirstInline is Hyperlink link)
            BindingOperations.SetBinding(link, Hyperlink.CommandProperty, CommandBinding);

        return result;
    }
}

After setting up the correct namespace mapping you can then do this:

<c:DataGridHyperlinkColumn Header="Booking" Binding="{Binding Path=ReservationNo}" CommandBinding="{Binding Path=NavigateCommand}" />

This assumes that NavigateCommand exists on your viewmodel object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文