我是 WPF 新手,我发现了一些类似的问题,但不太清楚最后一部分。我有一个带有 ObservableCollection 的 ViewModel,其中包含错误消息。我想在表单上显示这些消息并允许用户选择和复制全部或部分消息。 (过去在 WinForm 应用程序中,我为此使用了 RichTextBox,但我不知道如何将其绑定到 WPF 中的集合。)
我通过以下 xaml 获得了想要的外观,但是没有内置的方法可以像使用 RichTextBox 那样进行选择和复制。有谁知道我应该使用哪个控件,或者是否有方法可以选择/复制所有 TextBlock 的内容,或者是否可以将其绑定到 RichTextBox?
<Grid Margin="6">
<ScrollViewer VerticalScrollBarVisibility="Auto" Height="40" Grid.Column="0" Margin="6">
<ItemsControl ItemsSource="{Binding ErrorMessages}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Mode=OneWay}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
[编辑]
@Andrey Shvydky - 这不适合评论。
我花了一段时间才弄清楚正确的语法(尤其是 /, 东西),但最终我得到了如下所示的流文档语法。它在表单上看起来是正确的,乍一看似乎支持全选/复制。但是当我在全选/复制后粘贴时,什么也没有显示。有人知道为什么吗?
<Grid Margin="6">
<FlowDocumentScrollViewer>
<FlowDocument >
<Paragraph>
<ItemsControl ItemsSource="{Binding ErrorMessages, Mode=OneWay}" />
<Run Text="{Binding /, Mode=OneWay}" />
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
</Grid>
除非你有大量的消息转换器可能可行:
<TextBox IsReadOnly="True">
<TextBox.Text>
<Binding Path="Messages" Mode="OneWay">
<Binding.Converter>
<vc:JoinStringsConverter />
</Binding.Converter>
</Binding>
</TextBox.Text>
</TextBox>
public class JoinStringsConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var strings = value as IEnumerable<string>;
return string.Join(Environment.NewLine, strings);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)