DataSource更改时,DataGridRowGroupHeader中的Silverlight绑定不会更新

时间:2010-10-18 17:44:42

标签: silverlight xaml datagrid binding grouping

我在DataGridRowGroupHeader的内联样式中有一个Binding,就像这样。

<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}}" />

DataGrid ItemsSource绑定到包含可观察集合的PageCollectionView,该集合按集合中的属性进行分组。当我更新集合时,网格的行会发生变化,但GroupHeader中的绑定不会更改。

是否有不同的绑定方式或强制UI更新的方法?

这是我在Header绑定上使用的转换器:

 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));
    }
}

2 个答案:

答案 0 :(得分:0)

问题是您是直接绑定到对象而不是绑定到Path的属性。如果在没有Path的情况下绑定,绑定将永远不会更新,因为没有PropertyChanged事件通知UI绑定已更改。最简单的更改是将绑定更改为{Binding Items, Converter={StaticResource headerConverter}},然后将转换器中的值直接转换为ReadOnlyObservableCollection<object>

如果您需要更多灵活性,我相信您必须使用自定义ICollectionView实施自己的CollectionViewGroup

答案 1 :(得分:0)

斯蒂芬

我遵循你的解决方案有所不同: - 我使用参数 - 我使用datagridcell

但是当我修改我的数据源时,datagroupheader不会改变。 <data:DataGridCell Content="{Binding Items,Converter={StaticResource myConverterBnc}, ConverterParameter=Fattura,UpdateSourceTrigger=PropertyChanged}" Foreground="White" Width="113"/>