显示可点击文本 WPF

发布于 2024-12-10 19:32:32 字数 479 浏览 0 评论 0原文

我想在 GUI 中显示一些文本,并让用户能够双击它。 我想抓住这个事件并处理它。

我想这样做:

 <TextBlock   
        Height="39" 
        TextElement.FontSize="18" 
        FontFamily="Verdana"
        HorizontalAlignment="Left"
        VerticalAlignment="Center"
        Name="Filelink"
        Padding="5,0,0,0"
        TextDecorations="Underline"
        Text="{Binding Path=FilePath}"/>

但似乎处理 TextBlock 中的点击并不容易。

任何想法什么是呈现可点击文本的最佳方式。

谢谢。

I want to present some text in the GUI and give the user that ability to double click it.
I want to catch this event and deal with it.

I thought to do it like this :

 <TextBlock   
        Height="39" 
        TextElement.FontSize="18" 
        FontFamily="Verdana"
        HorizontalAlignment="Left"
        VerticalAlignment="Center"
        Name="Filelink"
        Padding="5,0,0,0"
        TextDecorations="Underline"
        Text="{Binding Path=FilePath}"/>

But seems that it's not easy to deal with clicks in TextBlock .

Any ideas what is the best way to present a click able text.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

甜妞爱困 2024-12-17 19:32:32

如果您想要可点击的文本,您只需重新设置 Button 的样式:

<Button Content="Text here" Click="Click_Handler">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <ContentPresenter />
        </ControlTemplate>
    </Button.Template>
</Button>

另请参阅 这个问题

If you want clickable text you can just restyle a Button:

<Button Content="Text here" Click="Click_Handler">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <ContentPresenter />
        </ControlTemplate>
    </Button.Template>
</Button>

Also see this question.

一抹淡然 2024-12-17 19:32:32

您可以在文本块中嵌入超链接,如本示例所示

<TextBlock>
    <Hyperlink NavigateUri="Reviews.xaml">Click Me </Hyperlink>
</TextBlock>

您还可以处理超链接 Click 事件来调用 Navigate

You can embed a hyberlink in a Textblock as shown in this example

<TextBlock>
    <Hyperlink NavigateUri="Reviews.xaml">Click Me </Hyperlink>
</TextBlock>

You can also handle the hyperlinks Click event to call Navigate for example

偷得浮生 2024-12-17 19:32:32

为什么不使用 Label 并监听 MouseDoubleClick 事件(尽管我同意 Xin 关于可用性的评论)?

Why don't you just use a Label and listen for the MouseDoubleClick event (although I do agree with Xin's comment about usability)?

旧时模样 2024-12-17 19:32:32

如果使用 LabelHyperlink 在您的情况下不起作用,您可以采取创建新的派生 TextBlock 的方法,该方法只需定义一个新的 DoubleClick 路由事件,该事件在树中向上冒泡:

public class ClickableTextBlock : TextBlock
{
    #region Overrides

    protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);

        if (e.ClickCount == 2)
        {
            RaiseEvent(new RoutedEventArgs(DoubleClickEvent, this));
        }
    }

    #endregion

    #region DoubleClick RoutedEvent

    public static readonly RoutedEvent DoubleClickEvent = EventManager.RegisterRoutedEvent("DoubleClick",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ClickableTextBlock));

    public event RoutedEventHandler DoubleClick
    {
        add { AddHandler(DoubleClickEvent, value); }
        remove { RemoveHandler(DoubleClickEvent, value); }
    }

    #endregion
}

该控件的使用方式与标准 TextBlock 相同。在此示例中,双击 TextBlock 将引发 DoubleClick 事件,然后由父 StackPanel 执行该事件以启动动画:

<StackPanel x:Name="myStackPanel" Background="LightGreen">
        <StackPanel.Triggers>
            <EventTrigger RoutedEvent="l:ClickableTextBlock.DoubleClick">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                         Duration="0:0:1"
                                         To="0.5"
                                         FillBehavior="Stop"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </StackPanel.Triggers>
        <l:ClickableTextBlock HorizontalAlignment="Center"
                              VerticalAlignment="Center"
                              Background="LightPink"
                              Text="Double Click to change parent's opacity" />
    </StackPanel>

Hope这有帮助!

If using a Label or a Hyperlink won't work in your situation, you could take the approach of a creating a new derived TextBlock that simply defines a new DoubleClick routed event which bubbles up through the tree:

public class ClickableTextBlock : TextBlock
{
    #region Overrides

    protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
    {
        base.OnMouseLeftButtonDown(e);

        if (e.ClickCount == 2)
        {
            RaiseEvent(new RoutedEventArgs(DoubleClickEvent, this));
        }
    }

    #endregion

    #region DoubleClick RoutedEvent

    public static readonly RoutedEvent DoubleClickEvent = EventManager.RegisterRoutedEvent("DoubleClick",
        RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ClickableTextBlock));

    public event RoutedEventHandler DoubleClick
    {
        add { AddHandler(DoubleClickEvent, value); }
        remove { RemoveHandler(DoubleClickEvent, value); }
    }

    #endregion
}

This control can be used in the same way as your standard TextBlock. In this example, double clicking on the TextBlock will raise the DoubleClick event which is then acted upon by the parent StackPanel to start an animation:

<StackPanel x:Name="myStackPanel" Background="LightGreen">
        <StackPanel.Triggers>
            <EventTrigger RoutedEvent="l:ClickableTextBlock.DoubleClick">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                         Duration="0:0:1"
                                         To="0.5"
                                         FillBehavior="Stop"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </StackPanel.Triggers>
        <l:ClickableTextBlock HorizontalAlignment="Center"
                              VerticalAlignment="Center"
                              Background="LightPink"
                              Text="Double Click to change parent's opacity" />
    </StackPanel>

Hope this helps!

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