You need to enable JavaScript to run this app. WPF XAML 如何配置列表项内点击复制链接而非跳转的超链接?
优惠活动
大模型
产品
解决方案
定价
更多

WPF XAML 如何配置列表项内点击复制链接而非跳转的超链接?

实现方案
  • 首先修正原代码的结构错误:Hyperlink是文档内联元素,不能直接作为Grid的子元素,必须嵌套在TextBlock内部;同时它没有自定义的Text/Link属性,文本需通过内嵌的Run元素绑定,URI直接绑定到内置的NavigateUri属性即可。
  • 复用逻辑无需为每个列表项写单独的后台事件:通过附加属性封装通用的点击复制、拦截跳转逻辑,仅需写一次后台代码,XAML中给超链接加一行配置即可复用。

第一步:添加命名空间配置

在包含该样式的窗口/用户控件根节点,添加自定义命名空间:

xmlns:local="clr-namespace:你的项目实际命名空间"

第二步:写一次通用附加属性

将以下代码放到项目的任意.cs文件中,无需额外修改:

using System.Windows;
using System.Windows.Documents;

namespace 你的项目实际命名空间
{
    public static class HyperlinkAttach
    {
        public static readonly DependencyProperty CopyUriOnClickProperty = DependencyProperty.RegisterAttached(
            "CopyUriOnClick", typeof(bool), typeof(HyperlinkAttach), new PropertyMetadata(false, OnCopyUriOnClickChanged));

        private static void OnCopyUriOnClickChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is not Hyperlink link) return;
            if ((bool)e.NewValue)
            {
                // 点击时自动复制绑定的URI到系统剪贴板
                link.Click += (_, _) =>
                {
                    if (link.NavigateUri != null)
                    {
                        Clipboard.SetText(link.NavigateUri.ToString());
                    }
                };
                // 拦截默认导航行为,不会唤起系统浏览器
                link.RequestNavigate += (_, e) => e.Handled = true;
            }
        }

        public static void SetCopyUriOnClick(DependencyObject element, bool value) => element.SetValue(CopyUriOnClickProperty, value);
        public static bool GetCopyUriOnClick(DependencyObject element) => (bool)element.GetValue(CopyUriOnClickProperty);
    }
}

第三步:修正后的完整ListBoxItem样式

<Style x:Key="ArticleListboxItem" TargetType="ListBoxItem">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Foreground" Value="White" />
    <Setter Property="FontFamily" Value="{StaticResource DefaultFont}" />
    <Setter Property="FontWeight" Value="Regular" />
    <Setter Property="FontSize" Value="16" />
    <Setter Property="UseLayoutRounding" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Border x:Name="Border" UseLayoutRounding="{TemplateBinding UseLayoutRounding}" Padding="3" Background="{TemplateBinding Background}" BorderThickness="0" SnapsToDevicePixels="true">
                    <Grid Background="{TemplateBinding Background}" HorizontalAlignment="Stretch" UseLayoutRounding="{TemplateBinding UseLayoutRounding}" Visibility="Visible">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                        </Grid.ColumnDefinitions>
                        <Grid Grid.Column="0"/> <!-- 占位间隔列 -->
                        <Label Name="lblArticleId" Grid.Column="1" Content="{Binding Path=ArticleID}" Foreground="White" HorizontalContentAlignment="Left" FontSize="14"/>
                        <!-- 修正后的超链接部分 -->
                        <TextBlock Grid.Column="2" FontSize="14" Foreground="White">
                            <Hyperlink NavigateUri="{Binding Path=ArticleLink}" local:HyperlinkAttach.CopyUriOnClick="True">
                                <Run Text="{Binding Path=ArticleTitle}" />
                            </Hyperlink>
                        </TextBlock>
                        <Label Name="lblViews" Grid.Column="3" Content="{Binding Path=ArticleViews}" Foreground="White" HorizontalContentAlignment="Right" FontSize="14"/>
                        <Label Name="lblDate" Grid.Column="4" Content="{Binding Path=ArticleDate}" Foreground="White" HorizontalContentAlignment="Right" FontSize="14"/>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <!-- 原有无关样式触发逻辑,比如鼠标悬停时修改前景色 -->
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

效果说明

  • 列表项超链接自动显示绑定的文章标题
  • 点击超链接仅会把对应文章的URI复制到系统剪贴板,不会唤起浏览器
  • 所有数据绑定的列表项自动复用该逻辑,无需为每个超链接单独编写后台事件

内容的提问来源于stack exchange,提问作者David Shnayder

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.24 06:06:04