我有这样的DataGridRowGroupHeader的内联样式的Binding。当DataSource更改时,DataGridRowGroupHeader中的Silverlight绑定不会更新
<sdk:DataGrid.RowGroupHeaderStyles>
<Style TargetType="sdk:DataGridRowGroupHeader">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="sdk:DataGridRowGroupHeader">
<TextBlock Margin="4,0,0,0" Text="{Binding Converter={StaticResource headerConverter}}" />
数据网格ItemsSource绑定到含有可观察集合,其通过在集合中一个属性分组一个PageCollectionView。当我更新集合时,网格的行更改,但GroupHeader中的绑定不会更改。
是否有不同的方式来绑定这个或强制UI更新的方式?
这是我使用的头绑定的转换器:
public class GroupHeaderConverter2 : IValueConverter {
public object Convert(object value, System.Type targetType, object parameter, CultureInfo culture) {
var cvg = value as CollectionViewGroup;
return string.Format("({0} Remaining)", cvg.Items.Count((i) => ((CheckListEventDefinition)i).Complete == false && ((CheckListEventDefinition)i).Required == true));
}
public object ConvertBack(object value,
System.Type targetType,
object parameter,
CultureInfo culture) {
return null;
}
}
得到这个工作,通过改变源集合到我自己的扩展的ObservableCollection也监视的PropertyChanged元素然后引发CollectionChanged事件。
/// <summary> this collection is also monitoring the elements for changes so when PropertyChanged fires on any element, it will raise the CollectionChanged event</summary>
public class ObservableCollectionEx<T> : ObservableCollection<T> where T : INotifyPropertyChanged {
public ObservableCollectionEx(ObservableCollection<T> regularCollection) {
if (regularCollection != null) {
foreach (var item in regularCollection) {
this.Add(item);
}
}
}
public void RaiseCollectionChanged() {
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) {
Unsubscribe(e.OldItems);
Subscribe(e.NewItems);
base.OnCollectionChanged(e);
}
protected override void ClearItems() {
foreach (T element in this)
element.PropertyChanged -= handlePropertyChanged;
base.ClearItems();
}
private void Subscribe(IList iList) {
if (iList == null) return;
foreach (T element in iList)
element.PropertyChanged += handlePropertyChanged;
}
private void Unsubscribe(IList iList) {
if (iList == null) return;
foreach (T element in iList)
element.PropertyChanged -= handlePropertyChanged;
}
private void handlePropertyChanged(object sender, PropertyChangedEventArgs e) {
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
感谢这个想法,我实际上是通过将底层集合切换到我自己的扩展ObservableCollection来工作的,该集合还监视PropertyChanged包含的元素,然后引发CollectionChanged。 – Tom 2010-10-20 15:35:22