가능한 한 간단하게 만들려고 노력할 것입니다. 3 일 동안이 작업을 수행하고 여러 가지 옵션을 시도했지만 어떤 이유로 든 여전히 작동하지 않습니다. 나는 wpf에 상당히 새로운 vb.net에 관한 문제 다.DataGrid에서 그룹 합계 얻기
그래서 여러 개의 그룹으로 구성된 DataGrid가 있습니다. 이 코드는 다음과 같습니다. 나도 같은 데이터 그리드에서이 하나 아래 두 번째하는 GroupStyle을 가지고 있지만 그것은 질문에 정말 중요하지 않습니다 :
<CollectionViewSource x:Key="HoursViewSource">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Phase"/>
<PropertyGroupDescription PropertyName="Employee"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<local:GroupTotalConverter x:Key="GroupTotal"/>
와 데이터 그리드 :
이
은 UserControl.Resources 부분이다. 당신은 내가 그룹에서 항목 전달되는 변환기를 볼 수 있듯이<DataGrid Grid.Row="1" Grid.ColumnSpan="2" ItemsSource="{Binding Mode=OneWay}" IsReadOnly="True" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Width="80" Header="Date" Binding="{Binding TransDate, StringFormat=d}"/>
<DataGridTextColumn Width="80" Header="Period" Binding="{Binding Period}"/>
<DataGridTextColumn Width="80" Header="Hours" Binding="{Binding Hours, StringFormat=n}"/>
<DataGridTextColumn Width="200" Header="Comment" Binding="{Binding TransComment, StringFormat=n}"/>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Foreground="White" Margin="0,0,0,3">
<Expander.Header>
<DockPanel>
<TextBlock Text="{Binding Path=Name}"/>
<TextBlock Text="{Binding Path=Items, Converter={StaticResource GroupTotal}"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter>
<ItemsPresenter.Effect>
<DropShadowEffect/>
</ItemsPresenter.Effect>
</ItemsPresenter>
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
, 나는 전달받을 항목을 확인되었으며,이 제대로 작동합니다. 문제는 변환기에 있거나 적어도 내가 계속 진행하고 있다는 가정입니다. 전달 된 항목 컬렉션을 보면 DataGrid로 보내지는 모든 속성이 있습니다. 그러나 나는 변환기로 전송되는 데이터로 아무 것도 할 수 없다.
변환기 :
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
If TypeOf value Is ReadOnlyObservableCollection(Of Object) Then
Dim items = CType(value, ReadOnlyObservableCollection(Of Object))
Dim total As Decimal = 0
For Each gi As GroupItem In items
total += gi.Hours
Next gi
Return total.ToString()
End If
Return ""
그래서이 변환기는 내가 다른 질문에 발견, C#을에서 전환했다. 기본 DataGrid 소스에 대한 주요 수정을 통해 수행 할 수 있지만 원본 유형과 동일한 유형으로 캐스팅 할 수 없습니다. 나는 또한 단지 오류를 변환기에 전달되는 컬렉션의 속성을 가진 간단한 클래스에 캐스팅을 시도하고이 경우 "Hours"항목이 CollectionGroupViewInternal의 일부가 아니라고 말합니다. 코드를 단계별로 살펴보면 컬렉션에는 Hours = 속성이 있습니다.
나는 내 문제를 분명히하기를 바라지 만, 이것은 매우 쉬운 수정이라고 말해 주지만, 내 고집 센 성격 때문에 나는 이것에 많은 시간을 할애해야했다.
내가 원하는 것은 반복적으로 특정 열의 합계를 가져 와서 해당 그룹의 헤더의 TextBlock에 배치 할 수있는 변환기를 만드는 것입니다. 현재 작업중인 프로젝트에는 그룹화 된 수십 개의 DataGrid가 있습니다.
미리 도움을 주셔서 감사합니다.