gpt4 book ai didi

wpf - 如何将段落数据绑定(bind)到 TextBlock?

转载 作者:行者123 更新时间:2023-12-05 00:44:01 33 4
gpt4 key购买 nike

我将如何采取Paragraph对象并将它们数据绑定(bind)到 TextBlock 以在 DataTemplate 中使用?
一个普通的绑定(bind)什么都不做,只是一个 ToString()段落对象。

InLines 属性可以让我手动添加组成段落的 TextRun 的列表,但这不能绑定(bind),我真的可以使用基于绑定(bind)的解决方案。

编辑问题以专注于我真正需要做的事情。

最佳答案

下面是一个使用嵌套 ItemsControl 的示例。不幸的是,它会为每个内联创建一个 TextBlock,而不是将整个段落放入一个 TextBlock:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.Resources>
<FlowDocument x:Key="document">
<Paragraph><Run xml:space="preserve">This is the first paragraph. </Run><Run>The quick brown fox jumps over the lazy dog.</Run></Paragraph>
<Paragraph><Run xml:space="preserve">This is the second paragraph. </Run><Run>Two driven jocks help fax my big quiz.</Run></Paragraph>
<Paragraph><Run xml:space="preserve">This is the third paragraph. </Run><Run>Sphinx of black quartz, judge my vow!</Run></Paragraph>
</FlowDocument>
<DataTemplate DataType="{x:Type Paragraph}">
<ItemsControl ItemsSource="{Binding Inlines}" IsHitTestVisible="False">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>
</Grid.Resources>
<ListBox ItemsSource="{Binding Blocks, Source={StaticResource document}}"/>
</Grid>

如果您希望每个元素一个段落,您可能应该按照建议执行并使用只读 RichTextBox 或 do what this person did and derive from TextBlock以便可以绑定(bind) Inlines 属性。

关于wpf - 如何将段落数据绑定(bind)到 TextBlock?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/483834/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com