使用DataGridColumnHeader C#绑定DataGrid组头。

时间:2019-01-20 20:10:57

标签: c# wpf binding wpfdatagrid

我对C#和WPF还是陌生的,我一直在研究解决问题的方法已经有几个小时了。

因此,问题是: 我有这个DataGrid,它具有我已经完成的分组功能。 如果用户单击列标题,它将按代表该列的属性对项目进行分组。 我设法使组标题采用以下格式: “ {Group Name} {Items Count}”

我想使其成为这样: “ {Column Header}:{Group Name} {Items Count}”

问题是我没有做到这一点。

我有DataGrid的XAML,我真的希望我能在这里找到一个解决方案,并且我不会违反任何法律或重复任何事情,在此先感谢:

<DataGrid x:Name="testerDataGrid" IsReadOnly="True" Width="800" RowDetailsVisibilityMode="VisibleWhenSelected"
                  ItemsSource="{Binding TestersCollection}" EnableRowVirtualization="True" AutoGenerateColumns="False" Sorting="TesterDataGrid_Sorting" >

            <DataGrid.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Path=Name}"/>
                            </StackPanel>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                    <GroupStyle.ContainerStyle>
                        <Style TargetType="{x:Type GroupItem}">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                        <Expander>
                                            <Expander.Header>
                                                <StackPanel Orientation="Horizontal">
                                                    <StackPanel Orientation="Horizontal" Margin="0, 0, 0, 0">
                                                        <materialDesign:PackIcon Kind="ArrowDown"/>
                                                        <TextBlock Text="{Binding Path=Name}" Margin="0, 0, 10 , 0"/>
                                                        <TextBlock Text="("/>
                                                        <TextBlock Text="{Binding Path=ItemCount}" Margin="0,0,3,0"/>
                                                        <TextBlock Text="Trainees)"/>
                                                    </StackPanel>
                                                    <StackPanel Orientation="Horizontal" Margin="250, 0, 0, 0">
                                                        <materialDesign:PackIcon Kind="ArrowDown"/>
                                                        <TextBlock Text="{Binding Path=Name}" Margin="0, 0, 10 , 0"/>
                                                        <TextBlock Text="("/>
                                                        <TextBlock Text="{Binding Path=ItemCount}" Margin="0,0,3,0"/>
                                                        <TextBlock Text="Trainees)"/>
                                                    </StackPanel>
                                                    <StackPanel Orientation="Horizontal" Margin="250, 0, 0, 0">
                                                        <materialDesign:PackIcon Kind="ArrowDown"/>
                                                        <TextBlock Text="{Binding Path=Name}" Margin="0, 0, 10 , 0"/>
                                                        <TextBlock Text="("/>
                                                        <TextBlock Text="{Binding Path=ItemCount}" Margin="0,0,3,0"/>
                                                        <TextBlock Text="Trainees)"/>
                                                    </StackPanel>
                                                    <StackPanel Orientation="Horizontal" Margin="250, 0, 0, 0">
                                                        <materialDesign:PackIcon Kind="ArrowDown"/>
                                                        <TextBlock Text="{Binding Path=Name}" Margin="0, 0, 10 , 0"/>
                                                        <TextBlock Text="("/>
                                                        <TextBlock Text="{Binding Path=ItemCount}" Margin="0,0,3,0"/>
                                                        <TextBlock Text="Trainees)"/>
                                                    </StackPanel>
                                                    <StackPanel Orientation="Horizontal" Margin="250, 0, 0, 0">
                                                        <materialDesign:PackIcon Kind="ArrowDown"/>
                                                        <TextBlock Text="{Binding Path=Name}" Margin="0, 0, 10 , 0"/>
                                                        <TextBlock Text="("/>
                                                        <TextBlock Text="{Binding Path=ItemCount}" Margin="0,0,3,0"/>
                                                        <TextBlock Text="Trainees)"/>
                                                    </StackPanel>
                                                </StackPanel>
                                            </Expander.Header>
                                            <ItemsPresenter/>
                                        </Expander>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </GroupStyle.ContainerStyle>
                </GroupStyle>
            </DataGrid.GroupStyle>

            <DataGrid.ContextMenu>
                <ContextMenu>
                    <MenuItem x:Name="WorkTimeCmi" Header="Show Work Time" Click="WorkTimeCmi_Click">
                        <MenuItem.Icon>
                            <materialDesign:PackIcon Kind="Show"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <Separator/>
                    <MenuItem x:Name="AddCmi" Header="Add a New Tester" Click="AddCmi_Clicked">
                        <MenuItem.Icon>
                            <materialDesign:PackIcon Kind="Plus"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem x:Name="EditCmi" Header="Edit" Click="EditCmi_Clicked">
                        <MenuItem.Icon>
                            <materialDesign:PackIcon Kind="Edit"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem x:Name="RemoveCmi" Header="Remove" Click="RemoveCmi_Clicked">
                        <MenuItem.Icon>
                            <materialDesign:PackIcon Kind="Trash"/>
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </DataGrid.ContextMenu>

            <DataGrid.Columns>
                <DataGridTextColumn x:Name="idColumn" Width="SizeToCells" Header="Id" Binding="{Binding Id}"/>
                <DataGridTextColumn x:Name="firstNameColumn" Width="SizeToHeader" Header="First Name" Binding="{Binding FirstName}"/>
                <DataGridTextColumn x:Name="lastNameColumn" Width="SizeToHeader" Header="Last Name" Binding="{Binding LastName}"/>
                <DataGridTextColumn x:Name="genderColumn" Width="SizeToHeader" Header="Gender" Binding="{Binding Gender}"/>
                <DataGridTextColumn x:Name="dateOfBirthColumn" Width="SizeToHeader" Header="Date Of Birth" Binding="{Binding DateOfBirthStringFormat}"  IsReadOnly="True"/>
                <DataGridTextColumn x:Name="ageColumn" Width="SizeToHeader" Header="Age" Binding="{Binding Age}"/>
                <DataGridTextColumn x:Name="addressColumn" Width="SizeToCells" Header="Address" Binding="{Binding AddressStringFormat}"/>
                <DataGridTextColumn x:Name="phoneNumberColumn" Width="SizeToHeader" Header="Phone Number" Binding="{Binding PhoneNumber}"/>
                <DataGridTextColumn x:Name="emailAddressColumn" Width="SizeToCells" Header="Email Address" Binding="{Binding EmailAddress}"/>
                <DataGridTextColumn x:Name="carTypeColumn" Width="SizeToHeader" Header="Car Type" Binding="{Binding CarType}"/>
                <DataGridTextColumn x:Name="maximalDistanceColumn" Width="SizeToHeader" Header="Max. Dist." Binding="{Binding MaximalDistance}"/>
                <DataGridTextColumn x:Name="weeklyTestsCountColumn" Width="SizeToHeader" Header="Weekly Tests" Binding="{Binding WeeklyTestsCount}"/>
                <DataGridTextColumn x:Name="maximalWeeklyTestsColumn" Width="SizeToHeader" Header="Max. Weekly Tests" Binding="{Binding MaximalWeeklyTests}"/>
                <DataGridTextColumn x:Name="yearsOfExperienceColumn" Width="SizeToHeader" Header="Exp. (Years)" Binding="{Binding YearsOfExperience}"/>
            </DataGrid.Columns>
        </DataGrid>

0 个答案:

没有答案