通过转换器绑定到ObservableCollection的文本字符串未更新集合更改

时间:2017-07-13 14:41:15

标签: c# wpf binding observablecollection ivalueconverter

我有一个非常直接的IValueConverter,它将IList<string>转换为逗号分隔的字符串字符串。麻烦是集合(这是一个ObservableCollection)没有尝试更新文本,我可以告诉,因为IValueConverter中的调试点显示在加载初始绑定后没有调用它。

转换器(这部分在实际调用时似乎工作正常)

public class CollectionToCommaSeperatedString : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return parameter.ToString();
        if (((IList<string>)value).Count > 0)
            return String.Join(", ", ((IList<string>)value).ToArray()) ?? parameter.ToString();
        else
            return parameter.ToString();
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

绑定元素:

<TextBlock Text="{Binding SelectedChannels, ConverterParameter='(Click to select channels)', Converter={StaticResource CollectionToCommaSeperatedString}, ElementName=userControl, Mode=OneWay}" HorizontalAlignment="Left" VerticalAlignment="Top"/>

物业的CB:

    public ObservableCollection<string> SelectedChannels
    {
        get { return (ObservableCollection<string>)GetValue(SelectedChannelsProperty); }
        set { SetValue(SelectedChannelsProperty, value); }
    }

    public static readonly DependencyProperty SelectedChannelsProperty =
        DependencyProperty.Register("SelectedChannels", typeof(ObservableCollection<string>), typeof(ChannelSelector), new PropertyMetadata(new ObservableCollection<string>()));

1 个答案:

答案 0 :(得分:1)

实现所需的一种可能性是将转换器逻辑包装成可重用的行为。

这是一个,它将作为您的转换器工作,但仅适用于TextBlock.Text

public static class Behaviors
{
    public static ObservableCollection<string> GetTest(DependencyObject obj) => (ObservableCollection<string>)obj.GetValue(TestProperty);
    public static void SetTest(DependencyObject obj, ObservableCollection<string> value) => obj.SetValue(TestProperty, value);

    public static readonly DependencyProperty TestProperty =
        DependencyProperty.RegisterAttached("Test", typeof(ObservableCollection<string>), typeof(Behaviors), new PropertyMetadata(null, (d, e) =>
        {
            var textBlock = d as TextBlock;
            var collection = e.NewValue as ObservableCollection<string>;
            collection.CollectionChanged += (s, a) => 
            {
                // put logic here
                textBlock.Text = ... ;
            };
        }));
}

像这样使用:

<TextBlock local:Behaviors.Test="{Binding ...}" />

TODO:添加空检查,取消订阅(如果绑定到持久性ViewModel属性,可能会导致内存泄漏),正确命名......