2010-10-18 122 views
2

我有这样的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)); 
    } 
} 

回答

0

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

如果你需要更多的灵活性,那么我相信你将不得不实现你自己的ICollectionView定制CollectionViewGroup

+0

感谢这个想法,我实际上是通过将底层集合切换到我自己的扩展ObservableCollection来工作的,该集合还监视PropertyChanged包含的元素,然后引发CollectionChanged。 – Tom 2010-10-20 15:35:22

0

斯蒂芬

我遵循有区别您的解决方案: - 我使用参数 - 我使用datagridcell

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

相关问题