WPF向文本块添加边框

9fkzdhlc  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(222)

是否有可能添加一个边框到文本块。我需要它被添加在setter属性下面的代码:

<Style x:Key="notCalled" TargetType="{x:Type TextBlock}">
    <Setter Property="Margin" Value="2,2,2,2" />
    <Setter Property="Background" Value="Transparent" />
</Style>

字符串

pkwftd7m

pkwftd7m1#

你需要将你的文本块用边框包围起来。例如:

<Border BorderThickness="1" BorderBrush="Black">
    <TextBlock ... />
</Border>

字符串
当然,你也可以通过样式设置这些属性(BorderThicknessBorderBrush):

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

ncgqoxb0

ncgqoxb02#

TextBlock实际上并不从Control继承,因此它没有通常与Control关联的属性。在样式中添加边框的最佳方法是用Label替换TextBlock
有关TextBlock和其他控件之间的差异的更多信息,请参见this link

8yoxcaq7

8yoxcaq73#

<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"/>

相关问题