8

MVVMデザインパターンを使用してWPFTimeCardアプリを作成しており、ユーザーが1日ごとにグループ化して記録した合計(合計)時間を表示しようとしています。次のXAMLを使用して、すべてのTimeCardデータをグループに分割したListViewがあります。

<ListView.GroupStyle>
    <GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
        <GroupStyle.HeaderTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Name, StringFormat=\{0:D\}}" FontWeight="Bold"/>
                    <TextBlock Text="  (" FontWeight="Bold"/>
                    <!-- This needs to display the sum of the hours -->
                    <TextBlock Text="{Binding ???}" FontWeight="Bold"/>
                    <TextBlock Text=" hours)" FontWeight="Bold"/>
                </StackPanel>
            </DataTemplate>
        </GroupStyle.HeaderTemplate>
    </GroupStyle>
</ListView.GroupStyle>

これも可能ですか?最初は、CollectionViewGroupの部分的なクラスを作成し、独自のプロパティを追加すると思いました。しかし、これがうまくいくかどうかはわかりません。おそらくもっと良い解決策があります...何か提案はありますか?

4

3 に答える 3

19

e.tadeuが言ったことを拡張するために、HeaderTemplateのDataTemplateをCollectionViewGroupのItemsプロパティにバインドできます。これにより、現在のグループにあるすべてのアイテムが返されます。

次に、そのアイテムのコレクションから目的のデータを返すコンバーターを提供できます。あなたの場合、あなたはあなたが時間の合計が欲しいと言います。次のようなことを行うコンバーターを実装できます。

public class GroupHoursConverter : IValueConverter
{

    public object Convert(object value, System.Type targetType, 
                          object parameter, 
                          System.Globalization.CultureInfo culture)
    {
        if (null == value)
            return "null";

        ReadOnlyObservableCollection<object> items = 
              (ReadOnlyObservableCollection<object>)value;

        var hours = (from i in items
                     select ((TimeCard)i).Hours).Sum();

        return "Total Hours: " + hours.ToString();
    }

    public object ConvertBack(object value, System.Type targetType, 
                              object parameter, 
                              System.Globalization.CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}

次に、データテンプレートでこのコンバーターを使用できます。

    <Window.Resources>
        <local:GroupHoursConverter  x:Key="myConverter" />
    </Window.Resources>

    <ListView.GroupStyle>
        <GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Name, 
                                                  StringFormat=\{0:D\}}" 
                                   FontWeight="Bold"/>
                        <TextBlock Text="  (" FontWeight="Bold"/>
                        <!-- This needs to display the sum of the hours -->
                        <TextBlock Text="{Binding Path=Items, 
                                         Converter={StaticResource myConverter}}"
                                   FontWeight="Bold"/>
                        <TextBlock Text=" hours)" FontWeight="Bold"/>
                    </StackPanel>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
    </ListView.GroupStyle>

乾杯!

于 2010-02-10T16:47:44.150 に答える
1

コンバーターを使用します。

見る:

http://www.codeproject.com/KB/WPF/WPFAggregateConverter.aspx

これも役立つかもしれません:

http://www.codeproject.com/KB/WPF/DsxGridCtrl.aspx

于 2010-02-10T15:01:54.283 に答える
-2

ListViewグループ化に関するこのチュートリアルに従って、GroupStyleの「ItemCount」を使用して、含まれているアイテムの現在の数を表示するだけです。

于 2015-10-28T12:21:36.137 に答える