我试图弄清楚如何在 xaml 中构建递归绑定(bind)。我了解 HierarchialDataTemplate,但这不是我想要的,因为我的数据源不是项目的集合。具体来说,我正在构建一个异常浏览器,并试图找出表达异常的 InnerException 字段的最佳方式(这当然是另一个异常,因此是递归。)
这个异常浏览器是我正在构建的日志查看器的一部分。这是到目前为止 ListView 的 XAML:
<ListView x:Name="LogViewerOutput">
<ListView.ItemTemplate>
<DataTemplate DataType="Ushanka.Log.LogMessageEventArgs">
<Expander Style="{StaticResource ExceptionTreeStyle}" Width="Auto">
<Expander.Header>
<StackPanel Orientation="Horizontal">
<Image Stretch="Fill" Width="16" Height="16" Margin="5"
Source="{Binding Path=Level, Converter={StaticResource LogLevelIconConverter}}" />
<TextBlock Text="{Binding Message}" />
</StackPanel>
</Expander.Header>
<Expander.Content>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Exception.Message}" />
<TextBlock Text="{Binding Exception.Source" />
<!-- Here I would like to somehow be able to recursively browse through the tree of InnerException -->
</StackPanel>
</Expander.Content>
</Expander>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
有什么想法吗?这可能吗?
最佳答案
我将为异常创建一个 DataTemplate 并将 InnerException 绑定(bind)到其中的 ContentPresenter。当 InnerExpception 为 null 时,ContentPresenter 将停止链,并且您可以根据需要格式化异常。像这样的事情:
<DataTemplate DataType="{x:Type Exception}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Message}" />
<TextBlock Text="{Binding Source" />
<ContentPresenter Content="{Binding InnerException}" />
</StackPanel>
</DataTemplate>
关于c# - Wpf 递归绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1721216/