【问题标题】:wpf/xaml styling of a hyperlink that doesn't open the link but rather copies it to clipboard不打开链接而是将其复制到剪贴板的超链接的 wpf/xaml 样式
【发布时间】:2021-01-19 16:51:25
【问题描述】:

我有一个从网络检索文章的程序,基本上需要将所有文章显示在一个列表中,其中包含具有文章 ID、标题、uri、视图等的自定义项目。 我想将文章标题和 uri 合并为一个超链接,以便我看到标题,路径将是 uri,听起来像一个简单的超链接,但是,当我点击时,而不是直接在浏览器中打开链接它,我只是想让它把链接复制到我的剪贴板,这么多,我无法弄清楚该怎么做,如果它只是一次,我可以很容易地从代码和处理程序中完成它,但是因为它在一个列表,我需要从 xaml 完全配置它。 这是代码,我将它拆分,以便您可以准确地看到listItem样式的超链接在哪里,如果您能想到一个方法,那就太棒了。

<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"/> <!--Just a spacer-->>
                        <Label Name="lblArticleId" Grid.Column="1" Content="{Binding Path=ArticleID}" Foreground="White" HorizontalContentAlignment="Left" FontSize="14"/>

这是我需要帮助的部分,代码不正确,但更多是为了显示结构,而我没有任何失败的尝试干预(失败是指我没有关闭事件)

<Hyperlink Name="linkArticle" Grid.Column="2" Text="{Binding Path=ArticleTitle}" Link="{Binding Path=ArticleLink}"/>

和其余的listItem样式:

                        <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>
                    <!--Some color modifications that are irrelevant, like change foreground when mouse is over-->
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

【问题讨论】:

    标签: c# wpf xaml microsoft-metro


    【解决方案1】:

    以下 sn-p 显示了一个超链接,当单击该超链接时,该超链接将 Hyperlink.NavigateUri 复制到 Windows 剪贴板:

    <TextBlock>
      <Hyperlink NavigateUri="{Binding ArticleLink}" 
                 Click="CopyUriToClipboard_OnHyperlinkClicked">
        <Run Text="{Binding ArticleTitle}" />
      </Hyperlink>
    </TextBlock>
    
    private void CopyUriToClipboard_OnHyperlinkClicked(object sender, RoutedEventArgs e)
    {
      var hyperlink = sender as Hyperlink;
      Clipboard.SetText(hyperlink.NavigateUri.ToString(), TextDataFormat.Text);
    }
    

    【讨论】:

    • 所以我只需要将函数放在我使用列表框的 c# 文件中,它将适用于所有项目?
    • 您应该定义一个DataTemplate,而不是覆盖ControlTemplate。这是如何完成的。 ControlTemplate 旨在设计控件本身,在本例中为 ListBoxItem,例如在哪里显示内容。它还为控件添加了一些逻辑,例如突出显示。 DataTemplate 定义如何显示数据/内容。然后它将适用于具有正确类型的每个项目。定义一个DataTemplate 并将其分配给ListBox.ItemTemplate。 (Data Templating Overview)
    • 我不能说我知道这个概念和您所附的文档,至少从图片中它可能正是我正在寻找的。明天我会全部阅读,如果我仍然不清楚实施,我可以联系你寻求帮助吗?
    • 当然,你可以回来问你想要什么。库。
    • 我已经阅读了大部分内容并尝试实施,我可以从这里解决样式的想法,但是,我仍然不清楚如何更改每个项目的超链接的功能。 ..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 2012-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多