使用 List<string> 绑定 WPF 文本块 [英] WPF textblock binding with List<string>
问题描述
有谁知道是否有一种简单的方法可以将文本块绑定到列表.到目前为止,我所做的是创建一个列表视图并将其绑定到列表,然后我在列表视图中有一个使用单个文本块的模板.
does anyone know if there is a simple way to bind a textblock to a List. What I've done so far is create a listview and bind it to the List and then I have a template within the listview that uses a single textblock.
我真正想做的只是将 List 绑定到文本块并让它显示所有行.
what I'd really like to do is just bind the List to a textblock and have it display all the lines.
在 Winforms 中有一个Lines"属性,我可以将列表放入其中,但我没有在 WPF 文本块或文本框上看到它.
In Winforms there was a "Lines" property that I could just throw the List into, but I'm not seeing it on the WPF textblock, or TextBox.
有什么想法吗?
我错过了一些简单的东西吗?
did I miss something simple?
这是代码
<UserControl x:Class="QSTClient.Infrastructure.Library.Views.WorkItemLogView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="500" Height="400">
<StackPanel>
<ListView ItemsSource="{Binding Path=Logs}" >
<ListView.View>
<GridView>
<GridViewColumn Header="Log Message">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</StackPanel>
和工作项类
public class WorkItem
{
public string Name { get; set; }
public string Description { get; set; }
public string CurrentLog { get; private set; }
public string CurrentStatus { get; private set; }
public WorkItemStatus Status { get; set; }
public ThreadSafeObservableCollection<string> Logs{get;private set;}
我正在使用 Prism 创建控件并将其放入 WindowRegion
I'm using Prism to create the control and put it into a WindowRegion
WorkItemLogView newView = container.Resolve<WorkItemLogView>();
newView.DataContext = workItem;
regionManager.Regions["ShellWindowRegion"].Add(newView);
谢谢
推荐答案
将您的 List 转换为单个字符串,中间以 "作为分隔符.并将其绑定到 TextBlock.确保 TextBlock 不受其高度限制,以便它可以根据行数增长.我会将其实现为 XAML 绑定的值转换器,它将字符串列表转换为单个字符串,并在其间添加新行
Convert your List to a single string with " " as the delimiter in between. and bind that to the TextBlock. Make sure that the TextBlock is not restricted with its height , so that it can grow based on the number of lines. I would implement this as a Value Converter to XAML Binding which converts a List of strings to a single string with new line added in between
<TextBlock Text="{Binding Path=Logs,Converter={StaticResource ListToStringConverter}}"/>
ListToStringConverter 看起来像这样:
The ListToStringConverter would look like this:
[ValueConversion(typeof(List<string>), typeof(string))]
public class ListToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(string))
throw new InvalidOperationException("The target must be a String");
return String.Join(", ", ((List<string>)value).ToArray());
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
这篇关于使用 List<string> 绑定 WPF 文本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!