XAML WPF -无法调整列表视图列标题的大小

1u4esq0p  于 2022-12-31  发布在  其他
关注(0)|答案(1)|浏览(166)

我正在尝试在列表视图中启用列标题的大小调整,我的样式中有一些设置不正确,我看不到拖动和调整大小的选项。当我删除lib使用时,Everythink工作正常。样式资源:

<Style x:Key="ListViewItem" TargetType="ListViewItem">

        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#76b900"/>
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="#76b900"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="ListViewStyle" TargetType="{x:Type ListView}" >
        <Setter Property="ItemContainerStyle" Value="{StaticResource ListViewItem}"/>
    </Style>

    <Style x:Key="GridViewColumnHeaderStyle1" TargetType="{x:Type GridViewColumnHeader}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                    <Border BorderThickness="0,0,1,1" BorderBrush="Black" Background="Transparent">
                        <TextBlock x:Name="ContentHeader" Text="{TemplateBinding Content}" 
                                   Padding="5,5,5,5" Width="{TemplateBinding Width}" TextAlignment="Center" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="Foreground" Value="#76b900" />
        <Setter Property="FontFamily" Value="Segoe UI" />
        <Setter Property="FontSize" Value="18" />
        <Setter Property="Padding" Value="40,40" />

    </Style>

XAML中的"我的列表"视图:

<ListView  d:ItemsSource="{d:SampleData}" ItemsSource="{Binding PGroups}" SelectedItem="{Binding PGroupSelected}"
                             Foreground="White" Background="#292929" FontSize="16" BorderThickness="0"
                 Visibility="{Binding PGroupsVis}"  Style="{DynamicResource ListViewStyle}" 
                       ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="False" 
                       IsSynchronizedWithCurrentItem="True" >
                <ListView.View>
                    <GridView ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyle1}">
                        <GridViewColumn Header="Group" DisplayMemberBinding="{Binding Name}" Width="220" ></GridViewColumn>
                        <GridViewColumn Header="Test Level" DisplayMemberBinding="{Binding Level.Name}" Width="140"></GridViewColumn>
                        <GridViewColumn Header="OverrideColumn" DisplayMemberBinding="{Binding OverrideColumn}" Width="140"></GridViewColumn>
                        <GridViewColumn Header="Active" DisplayMemberBinding="{Binding IsActive}" Width="140"></GridViewColumn>
                        <GridViewColumn Header="Remove">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Button  ToolTip="Remove"
                               Command="{Binding Path=DataContext.RemovePGroupCommand, 
                               RelativeSource={RelativeSource AncestorType=ListView, 
                                               Mode=FindAncestor}}" CommandParameter="{Binding}"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center" Style="{StaticResource SimpleButton}">
                                        <Image Source="../Images/delete3.png" Width="25"
                   Height="25" Visibility="{Binding ComboUsersVis}" ></Image>

                                    </Button>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>

我试着用

<GridViewColumnHeader Content="Value" IsHitTestVisible="False"/>

但似乎不起作用。
我错过了什么?

z3yyvxxp

z3yyvxxp1#

您缺少用于调整页眉大小的缩略图。必须将其命名为PART_HeaderGripper:

<Thumb x:Name="PART_HeaderGripper"
             HorizontalAlignment="Right"
             Margin="0,0,-9,0"
             Style="{StaticResource GridViewColumnHeaderGripper}" />

当您重新模板化列标题时。

https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/listview-styles-and-templates?redirectedfrom=MSDN&view=netframeworkdesktop-4.8

<ControlTemplate TargetType="GridViewColumnHeader">
    <Grid>
      <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="CommonStates">
          <VisualState x:Name="Normal" />
          <VisualState x:Name="MouseOver">
            <Storyboard>
              <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).
                (GradientBrush.GradientStops)[1].(GradientStop.Color)"
                                            Storyboard.TargetName="HeaderBorder">
                <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource ControlMouseOverColor}" />
              </ColorAnimationUsingKeyFrames>
            </Storyboard>
          </VisualState>
          <VisualState x:Name="Pressed" />
          <VisualState x:Name="Disabled" />
        </VisualStateGroup>
      </VisualStateManager.VisualStateGroups>
      <Border x:Name="HeaderBorder"
              BorderThickness="0,1,0,1"
              Padding="2,0,2,0">
        <Border.BorderBrush>
          <LinearGradientBrush StartPoint="0,0"
                               EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
              <GradientStopCollection>
                <GradientStop Color="{DynamicResource BorderLightColor}"
                              Offset="0.0" />
                <GradientStop Color="{DynamicResource BorderDarkColor}"
                              Offset="1.0" />
              </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
          </LinearGradientBrush>

        </Border.BorderBrush>
        <Border.Background>

          <LinearGradientBrush StartPoint="0,0"
                               EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
              <GradientStopCollection>
                <GradientStop Color="{DynamicResource ControlLightColor}"
                              Offset="0.0" />
                <GradientStop Color="{DynamicResource ControlMediumColor}"
                              Offset="1.0" />
              </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
          </LinearGradientBrush>

        </Border.Background>
        <ContentPresenter x:Name="HeaderContent"
                          Margin="0,0,0,1"
                          RecognizesAccessKey="True"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
      </Border>
      <Thumb x:Name="PART_HeaderGripper"
             HorizontalAlignment="Right"
             Margin="0,0,-9,0"
             Style="{StaticResource GridViewColumnHeaderGripper}" />
    </Grid>
  </ControlTemplate>

在你创建模板之前,最好仔细看看默认的模板是什么。模板通常比你想象的要复杂得多。命名控件通常是为了在该控件的代码中引用它们。错过它们或者错误地命名它们,你很可能会遇到问题。

相关问题