Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>if you use the converter it works for the first time perfect, but if one or more logging comes to the logging list, there is no update on your binding, because the converter works only at the first time. all controls that are no item controls doesn't subscribe to the listchanged event!</p> <p>here is a little code for this scenario</p> <pre><code>using System; using System.Collections.ObjectModel; using System.Windows; namespace BindListToTextBlock { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { private WorkItem workItem; public MainWindow() { this.WorkItems = new ObservableCollection&lt;WorkItem&gt;(); this.DataContext = this; this.InitializeComponent(); } public class WorkItem { public WorkItem() { this.Logs = new ObservableCollection&lt;string&gt;(); } public string Name { get; set; } public ObservableCollection&lt;string&gt; Logs { get; private set; } } public ObservableCollection&lt;WorkItem&gt; WorkItems { get; set; } private void Button_Click(object sender, RoutedEventArgs e) { this.workItem = new WorkItem() {Name = string.Format("new item at {0}", DateTime.Now)}; this.workItem.Logs.Add("first log"); this.WorkItems.Add(this.workItem); } private void Button_Click_1(object sender, RoutedEventArgs e) { if (this.workItem != null) { this.workItem.Logs.Add(string.Format("more log {0}", DateTime.Now)); } } } } </code></pre> <p>the xaml</p> <pre><code>&lt;Window x:Class="BindListToTextBlock.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:BindListToTextBlock="clr-namespace:BindListToTextBlock" Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;Grid.Resources&gt; &lt;BindListToTextBlock:ListToStringConverter x:Key="ListToStringConverter" /&gt; &lt;/Grid.Resources&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition Height="Auto" /&gt; &lt;RowDefinition /&gt; &lt;/Grid.RowDefinitions&gt; &lt;Button Grid.Row="0" Content="Add item..." Click="Button_Click" /&gt; &lt;Button Grid.Row="1" Content="Add some log to last item" Click="Button_Click_1" /&gt; &lt;ListView Grid.Row="2" ItemsSource="{Binding Path=WorkItems}"&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Header="Name"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Path=Name}" /&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Header="Log Message"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding Path=Logs, Converter={StaticResource ListToStringConverter}}" /&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>the converter</p> <pre><code>using System; using System.Collections; using System.Globalization; using System.Linq; using System.Windows; using System.Windows.Data; namespace BindListToTextBlock { public class ListToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value is IEnumerable) { return string.Join(Environment.NewLine, ((IEnumerable)value).OfType&lt;string&gt;().ToArray()); } return "no messages yet"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return DependencyProperty.UnsetValue; } } } </code></pre> <p><strong>EDIT</strong></p> <p>here is a quick solution for the update propblem (this can be also made with a attached property)</p> <pre><code>public class CustomTextBlock : TextBlock, INotifyPropertyChanged { public static readonly DependencyProperty ListToBindProperty = DependencyProperty.Register("ListToBind", typeof(IBindingList), typeof(CustomTextBlock), new PropertyMetadata(null, ListToBindPropertyChangedCallback)); private static void ListToBindPropertyChangedCallback(DependencyObject o, DependencyPropertyChangedEventArgs e) { var customTextBlock = o as CustomTextBlock; if (customTextBlock != null &amp;&amp; e.NewValue != e.OldValue) { var oldList = e.OldValue as IBindingList; if (oldList != null) { oldList.ListChanged -= customTextBlock.BindingListChanged; } var newList = e.NewValue as IBindingList; if (newList != null) { newList.ListChanged += customTextBlock.BindingListChanged; } } } private void BindingListChanged(object sender, ListChangedEventArgs e) { this.RaisePropertyChanged("ListToBind"); } public IBindingList ListToBind { get { return (IBindingList)this.GetValue(ListToBindProperty); } set { this.SetValue(ListToBindProperty, value); } } private void RaisePropertyChanged(string propName) { var eh = this.PropertyChanged; if (eh != null) { eh(this, new PropertyChangedEventArgs(propName)); } } public event PropertyChangedEventHandler PropertyChanged; } </code></pre> <p>here is the usage for the <code>CustomTextBlock</code> (not tested)</p> <pre><code>&lt;TextBlock Text="{Binding Path=ListToBind, RelativeSource=Self, Converter={StaticResource ListToStringConverter}}" ListToBind={Binding Path=Logs} /&gt; </code></pre> <p>@Fueled hope this helps</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload