【问题标题】:Silverlight binding in a DataGridRowGroupHeader doesn't update when DataSource changes当 DataSource 更改时,DataGridRowGroupHeader 中的 Silverlight 绑定不会更新
【发布时间】:2011-04-27 02:00:44
【问题描述】:

我有一个像这样的 DataGridRowGroupHeader 的内联样式的绑定。

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

【问题讨论】:

    标签: silverlight xaml datagrid binding grouping


    【解决方案1】:

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

    如果您需要更大的灵活性,那么我相信您将不得不使用自定义 CollectionViewGroup 来实现自己的 ICollectionView

    【讨论】:

    • 感谢您的想法,我实际上通过将底层集合切换到我自己的扩展 ObservableCollection 来实现它,该集合还监视它包含的 PropertyChanged 元素,然后引发 CollectionChanged。
    【解决方案2】:

    斯蒂芬

    我遵循您的解决方案有所不同: - 我使用一个参数 - 我使用数据网格单元

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

    【讨论】:

      猜你喜欢
      • 2015-11-12
      • 1970-01-01
      • 2011-11-14
      • 2014-08-10
      • 2013-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      相关资源
      最近更新 更多