Regex 为什么TreeView在显示正则表达式匹配项时变为递归?
我试图制作一个用于测试正则表达式的应用程序 通过使用TreeView将MatchCollection显示到窗口 但它不能正常工作 App.xamlRegex 为什么TreeView在显示正则表达式匹配项时变为递归?,regex,wpf,recursion,treeview,datatemplate,Regex,Wpf,Recursion,Treeview,Datatemplate,我试图制作一个用于测试正则表达式的应用程序 通过使用TreeView将MatchCollection显示到窗口 但它不能正常工作 App.xaml <Application x:Class="RegularExpressionTester.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/
<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的结构造成的
Match实例本身相当于集合中的第一个对象,位于Match.Groups[0]
从
组实例相当于Captures属性返回的集合的最后一项,它反映了捕获组进行的最后一次捕获
从
如何修复?
按如下方式修改模板:
<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>
但它不能正常工作,无法解释问题所在?你能提供更多的细节吗?这很有效。但有些不同。似乎与铸造组匹配。最后,我使用IValueConverter谢谢你的帮助:)