2012-08-10 81 views
1

我有一个WPF与DataGrid和I组行(在示例中的“名称”)。我有每个组的汇总行(“名称”和我在转换器中计算的总金额)。 问题是我需要强制汇总行的绑定更新,但我无法访问它,只要它在DataTemplate中。从DataTemplate更新绑定

<DataGrid ItemsSource="{Binding Source={StaticResource posCurrencyOpen}}"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Amount" Binding="{Binding Amount, Converter {StaticResource Amount_Converter}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"> 
    </DataGrid.Columns> 
    <DataGrid.GroupStyle> 
     <GroupStyle> 
      <GroupStyle.HeaderTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal"> 
         <DataGridCell Content="{Binding Path=Name}"/> 
         <DataGridCell Content="{Binding Path=Items, Converter={StaticResource AmountGroup_Converter}, Mode=OneWay}"/> 
        </StackPanel> 
       </DataTemplate> 
      </GroupStyle.HeaderTemplate> 
     </GroupStyle> 
    </DataGrid.GroupStyle> 
</DataGrid> 

很多感谢您的帮助。

+1

为什么,什么时候“需要强制摘要行的绑定更新”? – franssu 2012-08-10 15:50:36

+0

'posCurrencyOpen'是'Position'的'ObservableCollection',我在'Position'上实现了'PropertyChanged'事件。但是,当我更改'金额',数据库更新,但不是“总额”'金额',所以我想强制绑定更新。 我在网上搜索了很多,人们说有效地它不会更新,但没有解决方案。 – Anthony 2012-08-13 12:26:43

回答

0

我想你需要在你的ICollectionView对象上调用Refresh()。

+0

谢谢,它与Refresh()一起工作,然后我终于找到了真正的解决方案。我需要在ObservableCollection上实现一个'PropertyChanged'事件。 – Anthony 2012-08-13 14:18:26

+0

谢谢你的帮助 – Anthony 2012-08-13 14:18:43

+0

不客气:) – franssu 2012-08-13 15:28:05

2

下面是我如何处理它:我有一个父对象包含按日期分组的子对象。每个孩子也有一个分配百分比属性(Pct)。我想按日期分组并显示Pct字段的小计,以便用户可以确保它们总计为100%。我在每个Child对象上插入了TotalPct属性,然后强制Child为同一日期的所有Children启动PropertyChanged事件。

public partial class Child: INotifyPropertyChanged 
    { 

    public double TotalPct 
    { 
     get 
     { 
      double ttl = 0; 
      if (Parent != null) 
      { 
       var peers = from d in Parent.Children where d.StartDate == StartDate select d; 
       foreach (Child dtl in peers) 
       { 
        ttl += dtl.Pct; 
       } 
      } 
      return ttl; 
     } 
    } 

    partial void OnPctChanged() 
    { 
     if (Parent != null) 
     { 
      var peers = from d in Parent.Children where d.StartDate == StartDate select d; 
      foreach (Child dtl in peers) 
      { 
       NotifyPropertyChanged("TotalPct"); 
      } 
     } 
    } 

然后我GroupStyle势必在itemcollection标题到第一个孩子的TotalPct属性:

<Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type GroupItem}"> 
        <cust:DataGrouper> 
         <cust:DataGrouper.Header> 
          <StackPanel Orientation="Horizontal" > 
           <TextBlock Text="{Binding Path=Name, StringFormat='MM/dd//yyyy'}"  
              Width="{Binding ElementName=detailsDataGrid, Path=Columns[0].ActualWidth}"/> 
           <TextBlock Text="" Width="{Binding ElementName=detailsDataGrid, Path=Columns[1].ActualWidth}" Margin="-20,0,0,0"/> 
           <TextBlock Text="" Width="{Binding ElementName=detailsDataGrid, Path=Columns[2].ActualWidth}" /> 
           <TextBlock Text="{Binding Path=Items[0].TotalPct, Converter={StaticResource percentStringToDouble}, ConverterParameter=2}" HorizontalAlignment="Right" 
              Width="{Binding ElementName=detailsDataGrid, Path=Columns[3].ActualWidth}"/> 
          </StackPanel> 
         </cust:DataGrouper.Header> 
         <ItemsPresenter /> 
        </cust:DataGrouper> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style>