【问题标题】:Underline a Label in C#在 C# 中为标签添加下划线
【发布时间】:2014-02-01 23:10:36
【问题描述】:

嘿,我尝试在 C# 中为标签添加下划线。我要做的是修改 Label 控件,使其看起来像一个超链接。

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

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

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

编辑:忘了说我使用的是 WPF 框架。

【问题讨论】:

  • 你必须使用label吗?你可以改用TextBlock 吗?
  • 我不知道。我想我可以使用 TextBlock,但是用标签不可能以某种方式实现这一点吗?
  • 看看TextBlock。您可以将 TextBlock 放在标签内。请参阅 TextBlock.TextDecorations

标签: c# xaml hyperlink label underline


【解决方案1】:

您不能使用带下划线的标签。请改用 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 项目后忽略。

【讨论】:

  • 很抱歉忘记提及我正在使用 WPF。
【解决方案3】:

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

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

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

【讨论】:

    【解决方案4】:

    我还是 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;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 2017-11-19
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多