WPF DataGrid扩展器中的分组行在右侧添加了额外的列,当所有扩展器都关闭时消失

sethlj11

另一个与WPF DataGrid有关的问题。

我有一个DataGrid,其行已分组,每个组都在Expander控件下。每行有两列。默认情况下,我打开了所有扩展器。第二列的宽度设置为“ *”,并且行标题已关闭。

当我关闭所有扩展器时,布局会更改最小的部分,将扩展器控件向左移动一点。当我打开任何一个扩展器时,它们都会向右移动最小的位。我还注意到,打开扩展器时,列标题在右侧显示了另一列,这使水平滚动条出现。

当扩展器打开时,如何调整datagrid声明以使此附加列不存在/出现?

<Grid>
  <ScrollViewer VerticalScrollBarVisibility="Auto"
                HorizontalScrollBarVisibility="Disabled" >
    <DataGrid AutoGenerateColumns="False"
              ItemsSource="{Binding Path=MyCollection}"
              GridLinesVisibility="None"
              CanUserAddRows="False"
              CanUserDeleteRows="False"
              CanUserReorderColumns="False"
              CanUserSortColumns="False"
              CanUserResizeColumns="False"
              CanUserResizeRows="False"
              HeadersVisibility="Column">
      <DataGrid.Resources>
        <ResourceDictionary>
          <Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}">
            <Setter Property="Background" Value="{x:Null}" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
            <Style.Triggers>
              <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{x:Null}" />
                <Setter Property="Foreground" Value="Black" />
                <Setter Property="BorderBrush" Value="{x:Null}" />
              </Trigger>
            </Style.Triggers>
          </Style>
          <Style x:Key="{x:Type DataGridRow}" TargetType="{x:Type DataGridRow}">
            <Setter Property="Margin" Value="4" />
            <Setter Property="Background" Value="{x:Null}" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
            <Style.Triggers>
              <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="{x:Null}" />
                <Setter Property="Foreground" Value="Black" />
                <Setter Property="BorderBrush" Value="{x:Null}" />
              </Trigger>
            </Style.Triggers>
          </Style>
        </ResourceDictionary>
      </DataGrid.Resources>
      <DataGrid.GroupStyle>
        <GroupStyle>
          <GroupStyle.Panel>
            <ItemsPanelTemplate>
              <DataGridRowsPresenter />
            </ItemsPanelTemplate>
          </GroupStyle.Panel>
          <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
              <Setter Property="Template">
                <Setter.Value>
                  <ControlTemplate TargetType="{x:Type GroupItem}">
                    <Expander Margin="4"
                              IsExpanded="True">
                      <Expander.Header>
                        <StackPanel Orientation="Horizontal"
                                    Margin="4">
                          <TextBlock FontWeight="Bold"
                                     FontSize="14"
                                     Text="{Binding Path=Name}" />
                        </StackPanel>
                      </Expander.Header>
                      <ItemsPresenter />
                    </Expander>
                  </ControlTemplate>
                </Setter.Value>
              </Setter>
            </Style>
          </GroupStyle.ContainerStyle>
        </GroupStyle>
      </DataGrid.GroupStyle>
      <DataGrid.Columns>
        <DataGridTemplateColumn Width="Auto">
        ...
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Width="*">
        ...
        </DataGridTemplateColumn>
      </DataGrid.Columns>
    </DataGrid>
  </ScrollViewer>
</Grid> 
sethlj11

我认为这部分是hack,部分是真实答案。

hack部分:为了不显示水平滚动条,我只是在DataGrid声明中将其禁用。然后,为确保该列不会在右侧被刮掉,我以用来关闭选择颜色的样式设置行边距和内边距。

实际部分:只要将最外面的网格的第二行的高度设置为“ *”(星号),将垂直滚动条设置为始终显示就可以正常工作。这样可以确保在打开扩展器并且要显示的行过多时,DataGrid将使用适当的空间量。

最终的DataGrid声明如下所示:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
  </Grid.RowDefinitions>

  <Label Content="Import Data Mappings"
         FontWeight="Bold"
         HorizontalAlignment="Center"
         Margin="4"
         Padding="4"
         Grid.Row="0" />
  <DataGrid AutoGenerateColumns="False"
            VerticalScrollBarVisibility="Visible"
            HorizontalScrollBarVisibility="Disabled"
            ItemsSource="{Binding Path=MyGroupedCollection, Mode=OneWay}"
            HeadersVisibility="Column"
            Grid.Row="1"
            ... >
    <DataGrid.Resources>
      <ResourceDictionary>
        <Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}">
          <Setter Property="Background" Value="White" />
          <Setter Property="BorderBrush" Value="{x:Null}" />
          <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
              <Setter Property="Background" Value="White" />
              <Setter Property="Foreground" Value="Black" />
              <Setter Property="BorderBrush" Value="{x:Null}" />
            </Trigger>
          </Style.Triggers>
        </Style>
        <Style x:Key="{x:Type DataGridRow}" TargetType="{x:Type DataGridRow}">
          <Setter Property="Margin" Value="0,4,0,4" />
          <Setter Property="Background" Value="White" />
          <Setter Property="BorderBrush" Value="{x:Null}" />
          <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
              <Setter Property="Background" Value="White" />
              <Setter Property="Foreground" Value="Black" />
              <Setter Property="BorderBrush" Value="{x:Null}" />
            </Trigger>
          </Style.Triggers>
        </Style>
      </ResourceDictionary>
    </DataGrid.Resources>

    <DataGrid.GroupStyle>
      <GroupStyle>
        <GroupStyle.Panel>
          <ItemsPanelTemplate>
            <DataGridRowsPresenter />
          </ItemsPanelTemplate>
        </GroupStyle.Panel>
        <GroupStyle.ContainerStyle>
          <Style TargetType="{x:Type GroupItem}">
            <Setter Property="Template">
              <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                  <Expander Margin="4"
                            IsExpanded="True">
                    <Expander.Header>
                      <StackPanel Orientation="Horizontal"
                                  Margin="4">
                        <TextBlock FontWeight="Bold"
                                   FontSize="14"
                                   Text="{Binding Path=Name}" />
                      </StackPanel>
                    </Expander.Header>
                    <ItemsPresenter />
                  </Expander>
                </ControlTemplate>
              </Setter.Value>
            </Setter>
          </Style>
        </GroupStyle.ContainerStyle>
      </GroupStyle>
    </DataGrid.GroupStyle>

    <DataGrid.Columns>
      <DataGridTemplateColumn Width="Auto">
        <DataGridTemplateColumn.HeaderTemplate>
          ...
        </DataGridTemplateColumn.HeaderTemplate>
        <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <TextBlock .../>
          </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
      <DataGridTemplateColumn Width="*">
        <DataGridTemplateColumn.HeaderTemplate>
          ...
        </DataGridTemplateColumn.HeaderTemplate>
        <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
            <DockPanel LastChildFill="True">
              <Button Style="{DynamicResource ResourceKey=MyButtonStyle}"
                      Content="Button Text"
                      Command="{Binding Path=MyButtonCommand}"
                      DockPanel.Dock="Left"/>
              <ComboBox Margin="4"
                        Padding="4"
                        SelectedItem="{Binding Path=MySelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                        ItemsSource="{Binding Path=MyAvailableFields}"
                        DockPanel.Dock="Left">
                <ComboBox.ItemTemplate>
                  <DataTemplate>
                    <TextBlock VerticalAlignment="Center"
                               HorizontalAlignment="Center"
                               Text="{Binding Path=MyDisplayName}" />
                  </DataTemplate>
                </ComboBox.ItemTemplate>
              </ComboBox>
            </DockPanel>
          </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
      </DataGridTemplateColumn>
    </DataGrid.Columns>
  </DataGrid>
</Grid>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

带有扩展器的WPF分组组合框

来自分类Dev

WPF扩展器未扩展

来自分类Dev

WPF扩展器未扩展

来自分类Dev

带多个按钮的WPF扩展器

来自分类Dev

带GridSplitter的WPF扩展器

来自分类Dev

带GridSplitter的WPF扩展器

来自分类Dev

WPF +扩展器控制问题

来自分类Dev

WPF-Stackpanel中有多个扩展器的问题

来自分类Dev

在WPF中,根据组合框输入创建扩展器

来自分类Dev

在WPF中,根据组合框输入创建扩展器

来自分类Dev

如何在 WPF 中为所有子节点显示可见的 treeviewItem 扩展器箭头?

来自分类Dev

WPF如何始终在TreeView上显示扩展器

来自分类Dev

WPF扩展器控件-转换的页眉宽度问题

来自分类Dev

使用触摸滑动来拖动打开WPF扩展器

来自分类Dev

WPF-网格扩展器-用餐空间

来自分类Dev

wpf-扩展器标题适合内容宽度?

来自分类Dev

扩展器内部列表框的VerticalScrollBar-WPF

来自分类Dev

ItemsControl中使用的WPF扩展器叠加层

来自分类Dev

无法使用样式禁用WPF扩展器

来自分类Dev

WPF动态创建的按钮和扩展器控件

来自分类Dev

在WPF扩展器控件上更改边框的拐角半径?

来自分类Dev

使用触摸滑动来拖动打开WPF扩展器

来自分类Dev

WPF扩展器覆盖元数据IsExpandedProperty

来自分类Dev

如何停止WPF控件(使用扩展器)在ItemControl中扩展到父级的高度

来自分类Dev

如何在WPF中将多个内容动态添加到扩展器

来自分类Dev

WPF扩展器-鼠标悬停时突出显示标题

来自分类Dev

WPF - 在页面上将 HorizontalAlignment 设置为 Left 时拉伸扩展器

来自分类Dev

具有多个功能按钮的C#WPF扩展器

来自分类Dev

WPF Tabcontrol 具有用于 tabitems 的扩展器功能

Related 相关文章

热门标签

归档