1

DataGrid を使用した WPF があり、行をグループ化します (例では「名前」で)。各グループの要約行があります (コンバーターで計算する「名前」と合計「金額」)。問題は、集計行のバインド更新を強制する必要があることですが、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>

多くの人があなたの助けに感謝します.

4

2 に答える 2

2

これが私がどのように取り組んだかです: 日付でグループ化された子オブジェクトを含む親オブジェクトがありました。各 Child には、割り当て率のプロパティ (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 の最初の Child の 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>
于 2012-09-13T17:51:28.290 に答える
0

ICollectionViewオブジェクトでRefresh()を呼び出す必要があると思います。

于 2012-08-13T13:23:42.220 に答える