我正在尝试使用 IValueConverter 将集合转换为代理对象以进行数据绑定。
转换器似乎工作正常,但问题是在集合中添加或删除新对象时。视图中没有刷新相同的..
模型对象:
public class A {
public ObservableCollection<string> Members { get; }
}
转换器
public class MemberConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
var collection = new CompositeCollection();
var a = value as A;
a.Members.ToList().ForEach(member => {
collection.Add(new ProxyClass{ A= a, Member= member });
});
return collection;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
throw new System.NotImplementedException();
}
}
代理类
public class ProxyClass {
public A A { get; set; }
public string Member{ get; set; }
}
XAML:
<DataTemplate DataType="{x:Type my:ProxyClass}">
<TextBlock Text="{Binding Path=Member}"/>
</DataTemplate>
<HierarchicalDataTemplate DataType="{x:Type A}" ItemsSource="{Binding Converter={StaticResource MemberConverter}}">
<TextBlock Text ="{Binding}"/>
</HierarchicalDataTemplate>