Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding ObservableCollection<> to a TextBox
    primarykey
    data
    text
    <p>I have data comming back from web service in the form of a <code>ObservableCollection&lt;string&gt;</code> I want to bind the collection to a read-only <code>TextBox</code> so that the user can select and copy the data to the clipboard.</p> <p>To get the collection bound to the Text property of the TextBox I created <code>IValueConverter</code> which converts the collection to a text string. This seems to work except that it only works once, it is as if the binding does not recognize subsequent changes to the Observable collection. Here is a simple application that reproduces the problem, just to confirm the binding is working correctly I also bind to a `ListBox'</p> <p>Is this because the Text binding simple does not handle the change events of the collection?</p> <p>One option would of course be for me to handle the collection changes and propogate those to a Text property that the TextBox is bound to, which is fine, but I would like to understand why what seemed to me to be an obvious solutions is not working as expected.</p> <p><strong>XAML</strong></p> <pre><code>&lt;Window x:Class="WpfTextBoxBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfTextBoxBinding" Title="MainWindow" Height="331" Width="402"&gt; &lt;StackPanel&gt; &lt;StackPanel.Resources&gt; &lt;local:EnumarableToTextConverter x:Key="EnumarableToTextConverter" /&gt; &lt;/StackPanel.Resources&gt; &lt;TextBox Text="{Binding TextLines, Mode=OneWay, Converter={StaticResource EnumarableToTextConverter}}" Height="100" /&gt; &lt;ListBox ItemsSource="{Binding TextLines}" Height="100" /&gt; &lt;Button Click="Button_Click" Content="Add Line" /&gt; &lt;/StackPanel &gt; &lt;/Window&gt; </code></pre> <p><strong>Code Behind</strong></p> <pre><code>using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Text; using System.Windows; using System.Windows.Data; using System.Globalization; namespace WpfTextBoxBinding { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public ObservableCollection&lt;string&gt; TextLines {get;set;} public MainWindow() { DataContext = this; TextLines = new ObservableCollection&lt;string&gt;(); // Add some initial data, this shows that the // TextBox binding works the first time TextLines.Add("First Line"); InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { TextLines.Add("Line :" + TextLines.Count); } } public class EnumarableToTextConverter : IValueConverter { public object Convert( object value, Type targetType, object parameter, CultureInfo culture) { if (value is IEnumerable) { StringBuilder sb = new StringBuilder(); foreach (var s in value as IEnumerable) { sb.AppendLine(s.ToString()); } return sb.ToString(); } return string.Empty; } public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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