我有一个非常简单的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>()));
实现所需目的的一种可能性是将转换器逻辑包装成可重用的行为。
这是一个,它将按照您的转换器应该工作,但仅适用于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 ...}" />
待办事项:添加空检查、取消订阅(如果绑定到持久 ViewModel 属性,可能会导致内存泄漏(、正确命名...