为什么 TreeView 在显示 Regex 匹配项时会递归?
Why TreeView becomes recursive when it displays Regex matches?
我尝试制作用于测试正则表达式的应用程序
通过使用 TreeView 将 MatchCollection 显示到 window
但是不能正常工作
App.xaml
<Application x:Class="RegularExpressionTester.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RegularExpressionTester"
xmlns:Regex="clr-namespace:System.Text.RegularExpressions;assembly=System"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:MainViewModel x:Key="MainViewModel"/>
<HierarchicalDataTemplate DataType="{x:Type Regex:Match}" ItemsSource="{Binding Path=Groups}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Match)}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type Regex:Group}" ItemsSource="{Binding Path=Captures}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Group)}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type Regex:Capture}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Capture)}"/>
</HierarchicalDataTemplate>
</Application.Resources>
结果:
我想这样显示(预期结果)
▶ 匹配
..▶ 群组
.....▶ 捕获
▶ 匹配
..▶ 群组
.....▶ 捕获
我该怎么办?
为什么可以得到无限层级?
这是由于 Match.Groups
和 Group.Captures
的结构
The Match instance itself is equivalent to the first object in the collection, at Match.Groups[0]
the Group instance is equivalent to the last item of the collection returned by the Captures property, which reflects the last capture made by the capturing group
如何解决?
像这样修改模板:
<HierarchicalDataTemplate DataType="{x:Type Regex:Match}"
ItemsSource="{Binding Path=Groups}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Match)}"/>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type Regex:Group}"
ItemsSource="{Binding Path=Captures}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Group)}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type Regex:Capture}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Capture)}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
我尝试制作用于测试正则表达式的应用程序
通过使用 TreeView 将 MatchCollection 显示到 window
但是不能正常工作
App.xaml
<Application x:Class="RegularExpressionTester.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:RegularExpressionTester"
xmlns:Regex="clr-namespace:System.Text.RegularExpressions;assembly=System"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:MainViewModel x:Key="MainViewModel"/>
<HierarchicalDataTemplate DataType="{x:Type Regex:Match}" ItemsSource="{Binding Path=Groups}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Match)}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type Regex:Group}" ItemsSource="{Binding Path=Captures}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Group)}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type Regex:Capture}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Capture)}"/>
</HierarchicalDataTemplate>
</Application.Resources>
结果:
我想这样显示(预期结果)
▶ 匹配
..▶ 群组
.....▶ 捕获
▶ 匹配
..▶ 群组
.....▶ 捕获
我该怎么办?
为什么可以得到无限层级?
这是由于 Match.Groups
和 Group.Captures
The Match instance itself is equivalent to the first object in the collection, at Match.Groups[0]
the Group instance is equivalent to the last item of the collection returned by the Captures property, which reflects the last capture made by the capturing group
如何解决?
像这样修改模板:
<HierarchicalDataTemplate DataType="{x:Type Regex:Match}"
ItemsSource="{Binding Path=Groups}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Match)}"/>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type Regex:Group}"
ItemsSource="{Binding Path=Captures}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Group)}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate DataType="{x:Type Regex:Capture}">
<TextBlock Text="{Binding Path=Value, StringFormat=[{0}](Capture)}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>