如何给文本块(TextBlock)添加边框?我需要在下面的代码setter属性中添加它:
<Style x:Key="notCalled" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="2,2,2,2" />
<Setter Property="Background" Value="Transparent" />
</Style>
如何给文本块(TextBlock)添加边框?我需要在下面的代码setter属性中添加它:
<Style x:Key="notCalled" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="2,2,2,2" />
<Setter Property="Background" Value="Transparent" />
</Style>
你需要将 TextBlock 放在一个 Border 中。例如:
<Border BorderThickness="1" BorderBrush="Black">
<TextBlock ... />
</Border>
当然,你也可以通过样式来设置这些属性(BorderThickness
、BorderBrush
):
<Style x:Key="notCalledBorder" TargetType="{x:Type Border}">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="Black" />
</Style>
<Border Style="{StaticResource notCalledBorder}">
<TextBlock ... />
</Border>
TextBlock不是从Control继承而来,因此它没有通常与Control相关的属性。如果您想在样式中添加边框,最好将TextBlock替换为Label。
有关TextBlock和其他控件之间差异的更多信息,请参见此链接。
Label
替换TextBlock
,但是你猜怎么着:Label
不支持绑定中的格式字符串。在XAML中,您可以添加格式字符串到标签绑定而不会导致语法错误,并且该程序编译和运行时没有异常。然而,标签只是不关心其绑定中的格式字符串。因此,我不得不采取将TextBlock
包装在Border
中的方法。真是一团糟... - Binarus<Label Name="Lbl_IP2" Style="{StaticResource LabelWithBorder}" Content="3"/>
<Style TargetType="Label" x:Key="LabelWithBorder">
<Setter Property="Padding" Value="10,5,10,5"/>
<Setter Property="Background" Value="#202020"/>
<Setter Property="Foreground" Value="#fff"/>
<Setter Property="Width" Value="auto"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
<Setter Property="FlowDirection" Value="LeftToRight"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Label}">
<Border SnapsToDevicePixels="true" Background="#000" BorderBrush="AliceBlue" BorderThickness="1" Padding="5">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True">
<ContentPresenter.Effect>
<DropShadowEffect BlurRadius="15" Direction="-90" RenderingBias="Quality" ShadowDepth="2" Opacity="0.3"/>
</ContentPresenter.Effect>
</ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
使用方法:
<Label Name="Lbl_IP2" Style="{StaticResource LabelWithBorder}" Content="3"/>