WPF文本块与列表&LT结合;串&GT; [英] WPF textblock binding with List<string>
问题描述
没有人知道是否有给TextBlock绑定到一个列表的简单方法。
什么我迄今所做的就是创建一个ListView并将其绑定到列表,然后我有一个使用一个单一的文本块列表视图中的模板。
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.
我真的很想做的只是绑定列表给TextBlock并将它显示的所有行。
what I'd really like to do is just bind the List to a textblock and have it display all the lines.
在的WinForms有,我可以随便扔名单成线属性,但我没有看到它在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?
这里的code
<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;}
我用棱镜来创建控件,并把它变成一个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);
感谢
推荐答案
通过将您列表以一个字符串为\\ r \\ n,如之间的分隔符。并绑定该到TextBlock。确保将TextBlock不与它的高度限制,因此,它可以长的基础上的行数。
我会实现这个作为一个值转换器到XAML绑定该字符串列表转换为一个字符串与新行
Convert your List to a single string with "\r\n" 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();
}
}
这篇关于WPF文本块与列表&LT结合;串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!