[WPF] 可選擇的TextBlock,正則匹配高亮顯示

孤独成派發表於2024-10-12

  實現TextBlock控制元件像TextBox一樣支援選擇文字,又保留TextBlock顯示省略號等特性,找不到參考文獻了,抱歉。

  下面演示基於可選擇TextBlock實現的效果:

程式碼已上傳至https://gitee.com/Pumbaa_Peng/textbook-demo.git

 1 <pp:ChromeWindow x:Class="TextBlockDemo.MainWindow"
 2                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3                  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 4                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 5                  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6                  xmlns:pp="https://www.cnblogs.com/pumbaa"
 7                  xmlns:behaviors="clr-namespace:TextBlockDemo.Behaviors"
 8                  xmlns:attach="clr-namespace:TextBlockDemo.Controls.Attach"
 9                  xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
10                  mc:Ignorable="d"
11                  Title="MainWindow" Height="720" Width="1024"
12                  UseLayoutRounding="True" SnapsToDevicePixels="True"
13                  Background="{DynamicResource PP.Brushes.Background}" Foreground="{DynamicResource PP.Brushes.Foreground}" FontSize="16" TextBlock.LineHeight="24">
14     <pp:ScrollViewerEx HorizontalScrollBarVisibility="Disabled">
15         <StackPanel Margin="18">
16             <TextBlock FontSize="18" LineHeight="36">示例一</TextBlock>
17             <TextBlock Style="{StaticResource App.Styles.TextBlock.Selectable}" Foreground="{DynamicResource PP.Brushes.Warning.Highlight}">這是普通的可選TextBlock</TextBlock>
18             <Line X2="1" Stretch="Uniform" Stroke="#999" Margin="0 18"></Line>
19             <TextBlock FontSize="18" LineHeight="36">示例二</TextBlock>
20             <TextBlock Foreground="{DynamicResource PP.Brushes.Warning.Highlight}" LineHeight="24">可選擇文字超出時顯示省略號,選擇時隱藏省略號</TextBlock>
21             <DockPanel Margin="0 12">
22                 <TextBlock Foreground="#666" VerticalAlignment="Center">繫結文字:</TextBlock>
23                 <TextBox x:Name="input" MinWidth="400" TextWrapping="Wrap" VerticalContentAlignment="Top" Text="在WPF(Windows Presentation Foundation)中,文字修整是一種常用的技術,用於處理文字內容超出其顯示區域時的情況。透過設定TextBlock的TextTrimming屬性,可以指定當文字超出其容器邊界時應如何顯示。這個屬性接受TextTrimming列舉的值,這些值決定了文字的修整行為。" />
24             </DockPanel>
25             <TextBox Style="{StaticResource App.Styles.TextBox.SelectableTextBlock}" MaxWidth="400" HorizontalAlignment="Left" Text="{Binding Text,ElementName=input}" />
26             <Line X2="1" Stretch="Uniform" Stroke="#999" Margin="0 18"></Line>
27             <TextBlock FontSize="18" LineHeight="36">示例三</TextBlock>
28             <TextBlock Foreground="{DynamicResource PP.Brushes.Warning.Highlight}" LineHeight="24">多行可選擇文字超出時顯示省略號,選擇時隱藏省略號</TextBlock>
29             <DockPanel Margin="0 12">
30                 <TextBlock Foreground="#666" VerticalAlignment="Center">行數:</TextBlock>
31                 <Slider x:Name="slider" Minimum="1" Maximum="10" Value="3" SmallChange="1" Width="400" HorizontalAlignment="Left"
32                         TickFrequency="1" TickPlacement="BottomRight" IsSnapToTickEnabled="True" />
33             </DockPanel>
34             <TextBox Style="{StaticResource App.Styles.TextBox.SelectableTextBlock.MaxLines}" MaxLines="{Binding Value,ElementName=slider}" MaxWidth="400" HorizontalAlignment="Left" Text="{Binding Text,ElementName=input}" />
35             <Line X2="1" Stretch="Uniform" Stroke="#999" Margin="0 18"></Line>
36             <TextBlock FontSize="18" LineHeight="36">示例四</TextBlock>
37             <TextBlock Foreground="{DynamicResource PP.Brushes.Warning.Highlight}" LineHeight="24">多行可選擇文字超出時顯示省略號,選擇時隱藏展開</TextBlock>
38             <TextBox Style="{StaticResource App.Styles.TextBox.SelectableTextBlock.AutoExpand}" MaxLines="{Binding Value,ElementName=slider}" Margin="0 12 0 0" MaxWidth="400" HorizontalAlignment="Left" Text="{Binding Text,ElementName=input}" />
39             <Line X2="1" Stretch="Uniform" Stroke="#999" Margin="0 18"></Line>
40             <TextBlock FontSize="18" LineHeight="36">示例五</TextBlock>
41             <TextBlock Foreground="{DynamicResource PP.Brushes.Warning.Highlight}" LineHeight="24">文字正則高亮顯示(可以多個正則,每個正則不同顏色)</TextBlock>
42             <TextBlock Margin="0 12 0 0" Style="{StaticResource App.Styles.TextBlock.Selectable}" TextWrapping="Wrap" attach:TextElement.Placeholder="{Binding Text,ElementName=input}">
43                 <i:Interaction.Behaviors>
44                     <behaviors:TextHighlightBehavior>
45                         <behaviors:TextHighlightBehavior.HighlightRules>
46                             <behaviors:HighlightRule Regex="[a-zA-Z]+" Brush="#F5201B" />
47                             <behaviors:HighlightRule Regex="文字|技術" Brush="#F69A18" />
48                         </behaviors:TextHighlightBehavior.HighlightRules>
49                     </behaviors:TextHighlightBehavior>
50                 </i:Interaction.Behaviors>
51             </TextBlock>
52             <Line X2="1" Stretch="Uniform" Stroke="#999" Margin="0 18"></Line>
53         </StackPanel>
54     </pp:ScrollViewerEx>
55 </pp:ChromeWindow>

相關文章