在C#中为标签加上下划线

问题描述 投票:0回答:4

[嘿,我试图在C#中为标签加上下划线。我正在尝试修改Label控件,使其看起来像超链接。

我尝试了很多东西,并浏览了许多站点/博客,但没有找到解决方案。我曾考虑过要使用TextBlock,但必须有一种使用Label控件的方法。

<Label Name="link" HorizontalAlignment="Stretch" VerticalAlignment="Center" Foreground="Blue"></Label>

希望您能帮助我,并感谢您的任何帮助。

编辑:忘记提及我正在使用WPF框架。

c# xaml hyperlink label underline
4个回答
5
投票

您不能在带有下划线的标签上使用。请改用TextBlock。除了TextBlock具有更好的样式选项之外,区别不是很明显。


2
投票

您可以使用链接标签:

http://msdn.microsoft.com/en-us/library/system.windows.forms.linklabel(v=vs.110).aspx

或:

this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

在您声明它是WPF项目之后,请忽略。


1
投票

如果您只想要一个链接,则可以使用HyperLink作为标签的内容。

HyperLink

它将在文本下划线,并将字体颜色设置为蓝色。但是,我不知道如何真正使其导航到该页面。抱歉。


0
投票

我还是WPF和C#的新手,但标签似乎包含一个内置文本块。我有一个项目,在主菜单上有很多按钮。每个按钮旁边都有一个标签。要求是更改标签的颜色,并在其旁边的按钮具有焦点时加下划线。

可能有一种更简单的方法可以做到这一点,但这就是我的实现方式:

  1. 我将每个标签-按钮对都包裹在一个堆栈面板中,这样,除了按钮(即标签)之外,只有一个孩子。

  2. 然后,我可以使用<Label x:Name="link"> <Hyperlink NavigateUri="http://www.stackoverflow.com"> Click here to go to StackOverflow </Hyperlink> </Label> UIHelper查找要添加和删除下划线的内置文本块。

希望这对某人有帮助。

XAML:

VisualTreeHelper

The ViewModel:

<StackPanel Grid.Row="1"
            Grid.ColumnSpan="2"
            Orientation="Horizontal"
            >
  <atris:AtrLabel Width="15"
                  HorizontalAlignment="Left"
                  Content="1"
                  Style="{StaticResource SomeStyle}"
                  />
  <atris:AtrButton Margin="0,0,0,0"
                   HorizontalAlignment="Left"
                   VerticalAlignment="Stretch"
                   VerticalContentAlignment="Top"
                   Command="{Binding SomeCommand}"
                   Content="Deposit"
                   >
    <i:Interaction.Triggers>
      <i:EventTrigger EventName="GotFocus">
        <cmd:EventToCommand Command="{Binding Path=SetLabelForeground}" PassEventArgsToCommand="True"/>
      </i:EventTrigger>
      <i:EventTrigger EventName="LostFocus">
        <cmd:EventToCommand Command="{Binding Path=RemoveLabelForeground}" PassEventArgsToCommand="True"/>
      </i:EventTrigger>
    </i:Interaction.Triggers>
  </atris:AtrButton>
</StackPanel>
© www.soinside.com 2019 - 2024. All rights reserved.