将ObservableCollection <>绑定到TextBox

Chr*_*lor 9 data-binding silverlight wpf ivalueconverter

我有以数据形式从Web服务返回的数据,ObservableCollection<string>我希望将集合绑定到只读,TextBox以便用户可以选择数据并将其复制到剪贴板.

要将集合绑定到我创建的TextBox的Text属性,该TextBox IValueConverter将集合转换为文本字符串.这似乎有效,除了它只工作一次,就好像绑定不能识别对Observable集合的后续更改.这是一个简单的应用程序,重现问题,只是为了确认绑定工作正常我还绑定到`ListBox'

这是因为Text绑定简单不处理集合的更改事件吗?

一个选项当然是我处理集合更改并将它们传播到TextBox绑定的Text属性,这很好,但我想理解为什么在我看来是一个明显的解决方案是不行的正如所料.

XAML

<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">
  <StackPanel>
    <StackPanel.Resources>
      <local:EnumarableToTextConverter x:Key="EnumarableToTextConverter" />
    </StackPanel.Resources>
    <TextBox Text="{Binding TextLines, Mode=OneWay, Converter={StaticResource EnumarableToTextConverter}}" Height="100" />
    <ListBox ItemsSource="{Binding TextLines}" Height="100" />
    <Button Click="Button_Click" Content="Add Line" />
  </StackPanel >
</Window>
Run Code Online (Sandbox Code Playgroud)

代码背后

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
{
  /// <summary>
  /// Interaction logic for MainWindow.xaml
  /// </summary>
  public partial class MainWindow : Window
  {
    public ObservableCollection<string> TextLines {get;set;}

    public MainWindow()
    {
      DataContext = this;

      TextLines = new ObservableCollection<string>();

      // 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();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Jul*_*ain 5

这是因为Text绑定简单不处理集合的更改事件吗?

确实.绑定仅在其源属性更改时更新.如果TextLines通过设置全新ObservableCollection和实现来更改属性INotifyPropertyChanged,则绑定将按预期工作.只有将新元素绑定到类似于ItemsControl.ItemsSource侦听集合更改的属性时,才能将新元素添加到集合中.

一个选项当然是我处理集合更改并将它们传播到TextBox绑定的Text属性,这很好.

那将是另一种解决方案.


Fue*_*led 5

稍微更优雅的方法是在Text属性上使用MultiBinding并绑定到Collection的Count属性.这将在每次集合的Count更改时更新绑定,并根据您定义的MultiValueConverter更新Text.

<TextBox>
    <TextBox.Text>
        <MultiBinding Converter="{x:Static l:Converters.LogEntryCollectionToTextConverter}">
            <Binding Path="LogEntries" Mode="OneWay"/>
            <Binding Path="LogEntries.Count" Mode="OneWay" />
        </MultiBinding>
    </TextBox.Text>
</TextBox>
Run Code Online (Sandbox Code Playgroud)

和转换器:

public static class Converters
{
    public static LogEntryCollectionToTextConverter LogEntryCollectionToTextConverter = new LogEntryCollectionToTextConverter();
}

public class LogEntryCollectionToTextConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        ObservableCollection<LogEntry> logEntries = values[0] as ObservableCollection<LogEntry>;

        if (logEntries != null && logEntries.Count > 0)
            return logEntries.ToString();
        else
            return String.Empty;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的用例中,我不允许TextBox更新它的源(因此'Model ="OneWay"'),但如果需要,转换器的ConvertBack方法将处理它.