我有一部分文字,其中一些单词格式化 . 这些文本列在ListBox中 . 当用户单击ListBoxitem时,我想收集该selectedItem并将用户带到另一个地方 . 我的问题是我无法将TextBlock与另一个TextBlock实例绑定 . TextBlock有许多内联,我想展示 .
我一直在尝试这个解决方案:
<ListBox Width="800" Name="foundedTextBlocksListBox" SelectionChanged="foundedTextBlocksListBox_SelectionChanged" Background="Transparent" ItemsSource="{Binding}" Grid.Row="2" Visibility="Visible" Height="Auto" HorizontalAlignment="Center">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Center" Orientation="Vertical">
<TextBlock x:Name="foundedTextBlocks" DataContext="{Binding Textblock}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在绑定到DataContext后,像这样:
ObservableCollection<FoundedTextBlock> listOfFoundedTextBlockResults = new ObservableCollection<FoundedTextBlock>();
TextBlock textblock = new TextBlock();
while (blockString.IndexOf("<b>") != -1)
{
int startOfWord = blockString.IndexOf("<b>");
int endOfWord = blockString.IndexOf("</b>");
string text = blockString.Substring(0, startOfWord);
textblock.Inlines.Add(text);
string boldedWord = blockString.Substring(startOfWord + 3, endOfWord - startOfWord - 3);
textblock.Inlines.Add(new Run() { Text = boldedWord, FontWeight = FontWeights.Bold });
blockString = blockString.Substring(endOfWord + 4);
textblock.Inlines.Add(blockString);
}
textblock.Tag = dbInfo;
listOfFoundedTextBlockResults.Add(new FoundedTextBlock() { Textblock = textblock });
}
foundedTextBlocksListBox.DataContext = listOfFoundedTextBlockResults;
我在ListBox中看不到任何ListBoxItems . 我的Binding是错的还是可能的?之前我管理过要显示的TextBlock.Text属性,但是在第一次内联添加到TextBlock之后,没有内联是粗体文本或任何其他内联 . 我怎么能解决这个annoiyng问题?简而言之,我需要显示许多带有格式化文本的TextBlock ...
FoundedTextBlock类有TextBlock textblock {get; set;}属性我保存到Tag属性我的类实例,所以我可以收集SelectedValueChanged事件发生时我需要的信息 .
1 回答
也许您应该在XAML中使用ContentPresenter而不是TextBlock
更换
同
请尝试一下......缺少其余代码以提供更好的答案 .