# 说在前面

其实并不是扩展了 TextBlock,而是使用了一个只读的 TextBox,修改了样式使之看起来像 TextBlock,效果如图
20250923102642.png

# 关键样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<Style
x:Key="SelectableTextBlock"
BasedOn="{StaticResource {x:Type TextBox}}"
TargetType="TextBox">
<Style.Resources>
<SolidColorBrush x:Key="TextControlBorderBrushPointerOver" Color="Transparent" />
<SolidColorBrush x:Key="TextControlBorderBrushFocused" Color="Transparent" />
<SolidColorBrush x:Key="TextControlBorderBrush" Color="Transparent" />
<SolidColorBrush x:Key="TextControlBorderBrushDisabled" Color="Transparent" />
<Style x:Key="TextControlContentHostStyle" TargetType="ScrollViewer">
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Background" Value="Transparent" />
</Style>
</Style.Resources>
<Setter Property="Background" Value="Transparent" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="TextWrapping" Value="Wrap" />
<Setter Property="Padding" Value="0" />
<Setter Property="Margin" Value="0" />
<Setter Property="MinHeight" Value="0" />
<Setter Property="FontFamily" Value="{DynamicResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{DynamicResource ControlContentThemeFontSize}" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="{DynamicResource TextControlForegroundDisabled}" />
</Trigger>
</Style.Triggers>
</Style>

# 修改内容

  1. 删除了获得焦点后的发光外边框
  2. 使容器高度缩减为文本高度
  3. 删除了禁用后的背景色

# 使用

1
<TextBox Style="{StaticResource SelectableTextBlock}" Text="{Binding Description}" />