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.
what I’d really like to do is just bind the List to a textblock and have it display all the lines.
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.
Any ideas?
did I miss something simple?
Here’s the 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>
and the WorkItem Class
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;}
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);
thanks
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
The ListToStringConverter would look like this: