如何只展开“SelectedItem”组的组合。在WPF数据网格中

时间:2017-06-02 09:22:27

标签: wpf datagrid grouping expander

我只想扩展datagrid的SelectedItem组,所有其他扩展器都将被折叠。 ' SelectedItem'从代码隐藏中选择。 该项目类型为' Vault_Item'并绑定到Property' RawMaterialItem' 使用过的事件是' .SelectionChanged':

Private Sub Dgv_RawMaterials_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles Dgv_RawMaterials.SelectionChanged
  If Dgv_RawMaterials.SelectedItem IsNot Nothing Then
    Dim SelectedMaterialItem As Vault_Item = DirectCast(Dgv_RawMaterials.SelectedItem, Vault_Item)
    _vm.RawMaterialItem = SelectedMaterialItem
    'What to do next?....
  End If
End Sub

这是我的GroupStyle:

  <Style x:Key="TextBlockStyle" TargetType="TextBlock">

  </Style>

  <Style x:Key="Group_HeaderStyle" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type GroupItem}">
          <Grid Width="{Binding ActualWidth,RelativeSource={RelativeSource TemplatedParent}}">
            <Expander  IsExpanded="False" Padding="5,2" Margin="0,0,0,0" Background="#FFD5DCE8">
              <Expander.Header>
                <StackPanel Orientation="Horizontal" Background="Transparent">
                  <TextBlock Text="{Binding Path=Name}"  Style="{DynamicResource TextBlockStyle}"/>
                  <TextBlock Text=" ("  FontWeight="DemiBold" Style="{DynamicResource TextBlockStyle}"/>
                  <TextBlock Text="{Binding Path=ItemCount}"  Style="{DynamicResource TextBlockStyle}"/>
                  <TextBlock Text=" "  FontWeight="DemiBold" Style="{DynamicResource TextBlockStyle}"/>
                  <TextBlock Text="Items"  FontWeight="DemiBold" Style="{DynamicResource TextBlockStyle}"/>
                  <TextBlock Text=")" FontWeight="DemiBold" Style="{DynamicResource TextBlockStyle}"/>
                </StackPanel>
              </Expander.Header>
              <Expander.Content>
                <ItemsPresenter/>
              </Expander.Content>
            </Expander>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

这是我的数据网格:

<TabItem x:Name="TabItem_RawMaterials" Header="Raw Materials">
            <DataGrid x:Name="Dgv_RawMaterials" Style="{DynamicResource DgvStyle}" HeadersVisibility="None">
              <DataGrid.GroupStyle>
                <GroupStyle ContainerStyle="{StaticResource Group_HeaderStyle}">
                  <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                      <DataGridRowsPresenter/>
                    </ItemsPanelTemplate>
                  </GroupStyle.Panel>
                </GroupStyle>
              </DataGrid.GroupStyle>
              <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Vault_Description}" Header="Description" CellStyle="{StaticResource LeftCellStyle}" CanUserResize="False" IsReadOnly="True" SortDirection="Ascending" Width="*" />
              </DataGrid.Columns>
            </DataGrid>
      </TabItem>

This is the result I want achieve

任何建议?

1 个答案:

答案 0 :(得分:0)

这是您问题的新解决方案,请根据您的要求更改其他内容:

<DataGridColumn>
<DataGridColumn.CellTemplate>
    <DataTemplate>
        <Expander Header="{Binding SomeText}">
            <TextBlock TextWrapping="Wrap" Text="{Binding SomeText}" />
        </Expander>
    </DataTemplate>
</DataGridColumn.CellTemplate>