[嘿,我试图在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项目之后,请忽略。
我还是WPF和C#的新手,但标签似乎包含一个内置文本块。我有一个项目,在主菜单上有很多按钮。每个按钮旁边都有一个标签。要求是更改标签的颜色,并在其旁边的按钮具有焦点时加下划线。
可能有一种更简单的方法可以做到这一点,但这就是我的实现方式:
我将每个标签-按钮对都包裹在一个堆栈面板中,这样,除了按钮(即标签)之外,只有一个孩子。
然后,我可以使用<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>