Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要显示具有自定义对齐方式的列表视图项_C#_.net_Wpf_Listview - Fatal编程技术网

C# 需要显示具有自定义对齐方式的列表视图项

C# 需要显示具有自定义对齐方式的列表视图项,c#,.net,wpf,listview,C#,.net,Wpf,Listview,我试图显示listview组项目,就像在附加的屏幕截图中一样,但在显示组视图项目时仍然面临问题 要求: 组标题需要以水平对齐方式显示,相应的组项目需要以垂直对齐方式显示 请为实现这一目标提出解决方案 您应该为GroupStyle定义Panel属性,并且可以删除ListView的ItemsPanel: <GroupStyle.Panel> <ItemsPanelTemplate> <StackPanel Orientation=&quo

我试图显示listview组项目,就像在附加的屏幕截图中一样,但在显示组视图项目时仍然面临问题

要求:

组标题需要以水平对齐方式显示,相应的组项目需要以垂直对齐方式显示


请为实现这一目标提出解决方案


您应该为GroupStyle定义Panel属性,并且可以删除ListView的ItemsPanel:

<GroupStyle.Panel>
    <ItemsPanelTemplate>
         <StackPanel Orientation="Horizontal" />
    </ItemsPanelTemplate>
</GroupStyle.Panel>

要更改方向,必须设置
GroupStyle.Panel
。由于您需要将项目安排为多行,因此应使用
WrapPanel

要使其正常运行,应禁用
列表视图的水平
滚动查看器
(以允许项目包装),并为
列表视图
提供固定高度(以使垂直
滚动查看器
可见)

由于不修改
GroupItem
ListView
的布局,因此可以安全地删除
GroupStyle.ContainerStyle
(至少是
Controltemplate
覆盖)和
ListView.ItemsPanel
模板覆盖。事实上,将
列表框
列表视图
ItemsPanelTemplate
显式设置为
StackPanel
,或通常设置为除
虚拟化面板
以外的其他对象,会取消对项目进行虚拟化的功能。UI虚拟化显著提高了性能,因此您不想禁用它

<ListView ItemsSource="{Binding Source={StaticResource cvs}}" 
          Height="400"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled">
  <ListView.GroupStyle>
    <GroupStyle>
      <GroupStyle.Panel>
        <ItemsPanelTemplate>
          <WrapPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
      </GroupStyle.Panel>
      <GroupStyle.HeaderTemplate>
        <DataTemplate>
          <TextBlock
            Padding="5,0,0,0"
            FontSize="14"
            FontWeight="Bold"
            Text="{Binding Name}" />
        </DataTemplate>
      </GroupStyle.HeaderTemplate>
    </GroupStyle>
  </ListView.GroupStyle>
</ListView>


感谢您的回答,它工作良好,需要对齐,但我仍然需要显示两行和相应的列,每列应包含两组,如最近在Description中附加的屏幕截图。我不认为您可以通过ListView直接实现这一点。我相信您需要第二个列表视图来创建第二行。或者可以添加其他列并将方向更改回垂直。但是,您必须准备好数据,以便放在不同的列中。这似乎有点像黑客。谢谢你的回答,我可以根据需要获得多行项目,只有组标题文本可见,组项目绑定不起作用。请检查屏幕截图。我没有看到任何错误?组项目在屏幕截图中可见。我误解你的意思了吗?
<Window.Resources>
    <CollectionViewSource x:Key='cvs' 
                  Source="{Binding Path=Products}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="ProductType" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>
<Grid>
    <ListView
    x:Name="listview"
    BorderThickness="0"
    ItemsSource="{Binding Source={StaticResource cvs}}"
    SelectedItem="{Binding SelectedProduct}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Product}" />
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock
                        Padding="5,0,0,0"
                        FontSize="14"
                        FontWeight="Bold"
                        Text="{Binding Name}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="GroupItem">
                                    <StackPanel Orientation="Vertical">
                                        <ContentPresenter/>
                                        <ItemsPresenter/>
                                    </StackPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
</Grid>
public class ProductCvs
{
    public string Product { get; set; }

    public string ProductType { get; set; }
}
<ListView ItemsSource="{Binding Source={StaticResource cvs}}" 
          Height="400"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled">
  <ListView.GroupStyle>
    <GroupStyle>
      <GroupStyle.Panel>
        <ItemsPanelTemplate>
          <WrapPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
      </GroupStyle.Panel>
      <GroupStyle.HeaderTemplate>
        <DataTemplate>
          <TextBlock
            Padding="5,0,0,0"
            FontSize="14"
            FontWeight="Bold"
            Text="{Binding Name}" />
        </DataTemplate>
      </GroupStyle.HeaderTemplate>
    </GroupStyle>
  </ListView.GroupStyle>
</ListView>