簡體   English   中英

在 C# 中給標簽加下划線

[英]Underline a Label in C#

嘿,我試圖在 C# 中給標簽加下划線。 我想要做的是修改 Label 控件,使其看起來像一個超鏈接。

我嘗試了很多東西並查看了許多網站/博客,但還沒有找到解決方案。 我想過使用 TextBlock 代替,但必須有一種方法可以使用 Label 控件來實現。

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

我希望你能幫助我,並感謝任何形式的幫助。

編輯:忘了提及我正在使用 WPF 框架。

您不能使用帶下划線的標簽。 請改用 TextBlock。 除了 TextBlock 更好的樣式選項之外,差異不是很明顯。

您可以使用鏈接標簽:

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 項目。

如果您只需要一個鏈接,您可以使用HyperLink作為標簽的內容。

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

它將在文本下划線並將字體顏色設置為藍色。 然而,我不知道如何讓它導航到那個頁面。 對於那個很抱歉。

我仍然是 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>

視圖模型:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM