WPF如何在实际折叠时设置DataGrid.Group标题文本

时间:2016-09-28 08:21:00

标签: c# wpf datagrid rows

我正在使用WPF应用程序,我使用<DataGrid.GroupStyle>对项目进行分组,我想知道如何在组折叠时设置标题文本。我将订单加载到该分组网格中,当我崩溃特定组时,我希望看到类似订单的数量:订单数量,因为更容易定位。现在我只看到订单数量,但我怎么能添加文字:

<DockPanel>
   <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" /> //+ some text
</DockPanel>

(此外,在我添加<DataGrid.Columns>之后,因为我想添加我的列manualy突然垂直滑块出现在屏幕上,即使没有更多项目左或右,它看起来很糟糕和烦人,我怎么能删除滑块,我想保留我的datagrid列,因为我想按照我的意愿设置它们,但我不想看到那个滑块:/?)

这是我的代码:

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="10*" />
</Grid.ColumnDefinitions>

<DataGrid Grid.Column="0" RowHeaderWidth="0" CanUserAddRows="False" AutoGenerateColumns="False"  x:Name="datagrid1" Margin="10,150,8,50" Background="Transparent" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" ItemsSource="{Binding}">

  <DataGrid.Resources>
    <Style TargetType="{x:Type DataGridColumnHeader}">
      <Setter Property="Background" Value="Black"/>
      <Setter Property="Opacity" Value="0.5"/>
      <Setter Property="Foreground" Value="White"/>
      <Setter Property="HorizontalContentAlignment" Value="Center" />
      <Setter Property="FontSize" Value="15"/>
      <Setter Property="FontFamily" Value="Arial"/>
      <Setter Property="Height" Value="50"/>
    </Style>
  </DataGrid.Resources>

  <DataGrid.Columns>
    <DataGridTextColumn Binding="{Binding ProductName}"      Header="Title"      MinWidth="50"   FontSize="16"  FontFamily="Verdana" />
    <DataGridTextColumn Binding="{Binding Quantity}"       Header="Quantity"    MinWidth="30"   FontSize="16"  FontFamily="Verdana" />
    <DataGridTextColumn Binding="{Binding NumberOfOrder}"    Header="Order number"  MinWidth="30"   FontSize="16"  FontFamily="Verdana" />
    <DataGridTextColumn Binding="{Binding User}"          Header="User"         Width="*"     FontSize="16"   FontFamily="Verdana" />
  </DataGrid.Columns>

  <DataGrid.GroupStyle>
    <!-- Style for groups at top level. -->
    <GroupStyle>
      <GroupStyle.ContainerStyle>
        <Style TargetType="{x:Type GroupItem}">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type GroupItem}">
                <Expander IsExpanded="True">
                  <Expander.Header>
                    <DockPanel>
                      <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" /> //I am wondering what does this line mean?
                    </DockPanel>
                  </Expander.Header>
                  <Expander.Content>
                    <ItemsPresenter />
                  </Expander.Content>
                </Expander>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </GroupStyle.ContainerStyle>
    </GroupStyle>
  </DataGrid.GroupStyle>
</DataGrid>

我的数据网格的垂直滑块@底部的P.S ScreenShot

enter image description here

1 个答案:

答案 0 :(得分:1)

我们需要Trigger如下所示,IValueConverter查找群组中的项目数量:

<Expander.Style>
    <Style TargetType="Expander">
        <Style.Triggers>
            <Trigger Property="IsExpanded" Value="True">
                <Setter Property="Header">
                    <Setter.Value>
                        <DockPanel>
                            <TextBlock FontWeight="Bold" Text="{Binding Name}" />
                        </DockPanel>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsExpanded" Value="False">
                <Setter Property="Header">
                    <Setter.Value>
                        <DockPanel>
                            <TextBlock FontWeight="Bold">
                              <TextBlock.Inlines>
                                 <Run Text="{Binding Name}"/>
                                 <Run Text=" ( "/>
                                   <Run Text="{Binding Name, Converter={StaticResource ItemCountCnvKey}}" />
                                 <Run Text=" ) "/>
                              </TextBlock.Inlines>
                            </TextBlock>
                        </DockPanel>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Expander.Style>

ItemCountConverter

public class ItemCountConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        CollectionViewGroup group = (CollectionViewGroup)value;            
        ReadOnlyObservableCollection<object> items = group.Items;
        return items.Count;
    }

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