在C#中为标签加下划线

时间:2014-01-14 00:57:07

标签: c# xaml hyperlink label underline

嘿,我试图在C#中标记一个Label。我想要做的是修改Label控件,使其看起来像一个超链接。

我尝试了很多东西并查看了许多网站/博客,但还没有找到解决方案。我考虑过使用TextBlock,但必须有一种方法可以使用Label控件。

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

我希望你能帮助我并感谢任何帮助。

编辑:忘了提到我正在使用WPF框架。

4 个答案:

答案 0 :(得分:5)

您不能使用带下划线的标签。请改用TextBlock。除了TextBlock的更好的样式选项之外,差异并不是很明显。

答案 1 :(得分: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项目。

答案 2 :(得分:1)

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

<Label x:Name="link">
    <Hyperlink NavigateUri="http://www.stackoverflow.com">
        Click here to go to StackOverflow
     </Hyperlink>
</Label>

它将为文本加下划线并将字体颜色设置为蓝色。但是,我不知道如何实际导航到该页面。对不起。

答案 3 :(得分:0)

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

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

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

  2. 然后我可以使用UIHelperVisualTreeHelper查找内置的文本块以添加和删除下划线。

希望这对某人有帮助。

XAML:

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

ViewModel:

protected sealed override void InitCommands()
  {
     SetLabelForeground = new RelayCommand<RoutedEventArgs>(SetForegroundOnLable);
     RemoveLabelForeground = new RelayCommand<RoutedEventArgs>(UnSetForegroundOnLable);
  }
   //changes the number back to gray and removes underline 
  private void UnSetForegroundOnLable(RoutedEventArgs obj)
  {
    Label closestLable = GetChildLabel(obj);
    closestLable.Foreground = new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#666666"));
    TextBlock childTextBlock = UIHelper.FindVisualChild<TextBlock>(closestLable);
    childTextBlock.TextDecorations.Remove(TextDecorations.Underline[0]);
  }
  //changes the number to blue and underlines it
  private void SetForegroundOnLable(RoutedEventArgs obj)
  {
    Label closestLable = GetChildLabel(obj);
    closestLable.Foreground = new System.Windows.Media.SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF009df3"));
    TextBlock childTextBlock = UIHelper.FindVisualChild<TextBlock>(closestLable);
    childTextBlock.TextDecorations.Add(System.Windows.TextDecorations.Underline);
  }
  //gets the number next to the button
  private static Label GetChildLabel(RoutedEventArgs obj)
  {
    Button buttonThatHasFocus = (Button)obj.OriginalSource;
    DependencyObject parent = VisualTreeHelper.GetParent(buttonThatHasFocus);
    Label closestLable = UIHelper.FindVisualChild<Label>(parent);
    return closestLable;
  }