我有這樣的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