根据Windows Applications Development with Microsoft .NET 4 70-511培训工具包
既然Label
控件和TextBlock
控件都是content控件并且只显示文本,那么它们之间的区别是什么?
发布于 2011-03-22 04:03:23
标签通常支持单行文本输出,而TextBlock则用于多行文本显示。
例如,在wpf中,TextBlock有一个启用多行输入的属性TextWrapping
;Label没有这个属性。
发布于 2011-03-22 04:08:18
Label
是ContentControl
,这意味着您可以将任何内容设置为它。任何东西,包括字符串、数字、日期、其他控件、图像、形状等,TextBlock
只能处理strings
。
发布于 2020-04-29 15:19:10
TextBlock
最烦人的特性可能是隐式样式查找行为,它的作用域仅限于最接近的DataTemplate
。这是非Control
xaml元素的默认行为。
<StackPanel Orientation="Vertical">
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
</Style>
<Style TargetType="Label">
<Setter Property="Foreground" Value="Red"/>
</Style>
</StackPanel.Resources>
<ContentControl Content="Test">
<ContentControl.ContentTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
<ContentControl Content="Test">
<ContentControl.ContentTemplate>
<DataTemplate>
<Label Content="{Binding}"/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</StackPanel>
产生以下结果:
你可以阅读更多关于它的here。
https://stackoverflow.com/questions/5382925
复制相似问题