Reputation: 41
Hey I have tried to underline a Label in C#. What I am trying to do is modifying the Label control so it looks like a hyperlink.
I have tried a lot of stuff and looked at many sites/blogs but haven't found a solution. I thought about using a TextBlock instead but there must be a way to do it with Label controls.
<Label Name="link" HorizontalAlignment="Stretch" VerticalAlignment="Center" Foreground="Blue"></Label>
I hope you can help me and appreciate any kind of help.
EDIT: Forgot to mention that I am using the WPF framework.
Upvotes: 0
Views: 9465
Reputation: 143
I am still new at WPF and C# but it appears that a label contains a built-in text block. I have a project with a lot of buttons on the main menu. Each button has a label next to it. The requirement was to change the color of the label and underline it when the button next to it has focus.
There is probably a way easier way to do this but this is how I accomplished it:
I wrapped each label-button pair into a stack panel so that there was only one child other than the button (which is the label).
Then I can use UIHelper
and VisualTreeHelper
to find the built in text block to add and remove the underline.
Hope this helps someone.
The 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>
The 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;
}
Upvotes: 0
Reputation: 2372
If all you want is a link, you can use a HyperLink
as the content for a label.
<Label x:Name="link">
<Hyperlink NavigateUri="http://www.stackoverflow.com">
Click here to go to StackOverflow
</Hyperlink>
</Label>
It will underline the text and set the font color to a blue color. I don't know how to actually make it navigate to that page, however. Sorry about that.
Upvotes: 1
Reputation: 1712
You can not use a Label with underline. Use a TextBlock instead. The difference is not very noticeable besides the better styling options for a TextBlock.
Upvotes: 5
Reputation: 176
You can use a Linked Label:
http://msdn.microsoft.com/en-us/library/system.windows.forms.linklabel(v=vs.110).aspx
Or:
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
Disregard after your edit stating it's a WPF project.
Upvotes: 2