WPF GridViewRowPresenter 把所有内容变成Textblocks

WPF GridViewRowPresenter turns all the content into Textblocks

我正在尝试创建一个功能齐全的列表 headers,但我似乎只能得到一个功能齐全的列表 headers,并且所有列表视图项目都缩减为文本块。或者一个列表视图,其中所有按钮和文本都具有完全样式,但 headers 没有被正确链接。见下图;

我的 ListView 定义对于它们两个都是这样的:

              <ListView ItemsSource="{Binding ScenarioComponents}" Style="{StaticResource ListViewWithHeader}" ItemContainerStyle="{StaticResource ListViewWithHeaderItem}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="Auto" Header="{Binding NameHeader}" DisplayMemberBinding="{Binding Name}"/>
                            <GridViewColumn Width="Auto" Header="{Binding TypeHeader}" DisplayMemberBinding="{Binding Type}"/>
                            <GridViewColumn Width="Auto" Header="{Binding InfoHeader}"/>
                            <GridViewColumn Width="Auto" Header="{Binding ExportHeader}"/>
                            <GridViewColumn Width="Auto" Header="{Binding DeleteHeader}"/>
                        </GridView>
                    </ListView.View>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="5*"/>
                                    <ColumnDefinition Width="1.5*"/>
                                    <ColumnDefinition Width="0.5*"/>
                                    <ColumnDefinition Width="0.5*"/>
                                    <ColumnDefinition Width="0.5*"/>
                                </Grid.ColumnDefinitions>
                                <Label Grid.Column="0" Content="{Binding Name}" HorizontalAlignment="Left"/>
                                <Label Grid.Column="1" Content="{Binding Type}" Background="{StaticResource XVROrange}"/>
                                <Button Grid.Column="2" Style="{StaticResource BorderLessButton}" Command="{Binding MoreInfoCommand}">
                                    <Button.Content>
                                        <Image Source="/Views/Images/cc2_gui_information_default.png"/>
                                    </Button.Content>
                                </Button>
                                <Button Grid.Column="3" Style="{StaticResource BorderLessButton}" Command="{Binding ExportCommand}">
                                    <Button.Content>
                                        <Image Source="/Views/Images/cc2_gui_export_default.png"/>
                                    </Button.Content>
                                </Button>
                                <Button Grid.Column="4" Style="{StaticResource BorderLessButton}" Command="{Binding DeleteCommand}">
                                    <Button.Content>
                                        <Image Source="/Views/Images/cc2_gui_b_delete.png"/>
                                    </Button.Content>
                                </Button>
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

样式的唯一区别是 Headers 工作的样式在 ListViewItem 中使用 GridViewRowPresenter,如下所示:

        <Style x:Key="ListViewWithHeaderItem" TargetType="ListViewItem">
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="Border" Padding="0" Background="Transparent" BorderBrush="{StaticResource XVR70}" BorderThickness="1">
                        <GridViewRowPresenter Content="{TemplateBinding Content}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

而第二个使用相同的样式,仅使用 ContentPresenter 而不是 GridViewRowPresenter,没有任何修饰。

         <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Background="Transparent" BorderBrush="{StaticResource Black47}" BorderThickness="0,0,0,0">
                        <ContentPresenter />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

出于某种原因,使用 GridViewRowPresenter 意味着我的 ListView.ItemTemplate 的 DataTemplate gets turned into a Textblock 中的每个项目。我不知道为什么。我真的更愿意继续使用 GridViewRowPresenter,因为这意味着 headers 绑定到列表中的项目并与它们和所有内容一起缩放。

有没有人知道我如何设计它才能工作?

能否将 ItemTemplate 中的模板放入 GridViewColumns 中?

<ListView.View>
  <GridView>
    <GridView.Columns>
      <GridViewColumn Width="Auto" Header="{Binding NameHeader}" DisplayMemberBinding="{Binding Name}">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <Label Grid.Column="0" Content="{Binding Name}" HorizontalAlignment="Left"/>
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
      <!-- more columns -->
    </GridView.Columns>
  </GridView>
</ListView.View>