WPF GridViewRowPresenter将所有内容转换为Textblocks

时间:2017-08-01 14:35:02

标签: c# wpf xaml listviewitem contentpresenter

我试图创建一个包含功能齐全的标题的列表,但我似乎只能得到一个包含功能标题的列表,并且所有列表视图项都缩减为textblocks。或者列表视图,所有按钮和文本都完全样式化,但没有正确链接标题。见下图;

enter image description here

我的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,因为这意味着标题绑定到列表中的项目并随之扩展。

有没有人知道如何设计这个样式?

1 个答案:

答案 0 :(得分:0)

您可以将模板从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>