wpf - 使用 WrapPanel 样式化 ListView.GroupStyle

标签 wpf xaml listview styles controltemplate

好的,这是我非常简单的问题。

我有一个 ListView我的样式是为了使它看起来像 Windows 资源管理器。

现在,我想将里面的项目分组。因此,我定义了一个 GroupStyleExpander将其分组。分组现在很好。

我不喜欢现在,我的ListView在单独的行上显示每个组,而我想要一些扩展器包装以便在同一行上显示许多组。

我猜图像比一些文本好。

这是我所拥有的:

What I Have

这是我想要的:

What I want

我找不到我应该为哪个属性设置样式才能制作 GroupItems放入 WrapPanel 中,就像我为这些项目所做的一样。

这是我的 ListView 样式:

<ResourceDictionary>

                    <!-- Explorer-style layout-->
                    <DataTemplate x:Key="ExplorerView">
                        <StackPanel Orientation="Horizontal" Height="Auto" Width="150">
                            <Image Source="{Binding Path=Value.AppConfig.Appli.AppType, Converter={StaticResource TypeToIconConverter}}" Margin="5"
                                   Height="50" Width="50"/>
                            <StackPanel VerticalAlignment="Center" Width="90">
                                <TextBlock Text="{Binding Path=Value.AppConfig.Appli.AppName}" 
                     FontSize="13" HorizontalAlignment="Left" TextWrapping="WrapWithOverflow"
                     Margin="0,0,0,1" />
                                <TextBlock Text="{Binding Path=Value.AppConfig.Appli.AppType}" FontSize="9" 
                     HorizontalAlignment="Left" Margin="0,0,0,1" />
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>

                    <!-- Group header style-->
    <Style x:Key="GroupHeaderStyle" TargetType="{x:Type GroupItem}">


        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">

                    <Expander x:Name="exp" IsExpanded="True" Width="310"
                                   BorderBrush="CornflowerBlue">

                        <Expander.Header>
                            <DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                                        Background="CornflowerBlue" x:Name="expContent"
                                        Width="{Binding RelativeSource={RelativeSource
                                            Mode=FindAncestor, AncestorType={x:Type Expander}},
                                            Path=Width}"
                                        Height="{Binding RelativeSource={RelativeSource
                                            Mode=FindAncestor, AncestorType={x:Type ToggleButton}},
                                            Path=ActualHeight}">
                                <CheckBox IsChecked="False" DockPanel.Dock="Right"/>
                                <TextBlock Text="{Binding Path=Name}" Foreground="White"
                                           FontWeight="Bold" HorizontalAlignment="Center" />
                            </DockPanel>
                        </Expander.Header>
                        <ItemsPresenter />
                    </Expander>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>



                </ResourceDictionary>
    <!-- (...) -->

    <ListView ItemsSource="{Binding GroupedConfig, Mode=TwoWay}" 
              ItemTemplate="{StaticResource ExplorerView}">



        <ListView.ItemsPanel>
            <ItemsPanelTemplate >
                <WrapPanel Width="{Binding (FrameworkElement.ActualWidth),
                     RelativeSource={RelativeSource 
                                     AncestorType=Expander}}"
                     ItemWidth="{Binding (ListView.View).ItemWidth,
                     RelativeSource={RelativeSource AncestorType=ListView}}"

                     ItemHeight="{Binding (ListView.View).ItemHeight,
                     RelativeSource={RelativeSource AncestorType=ListView}}" />
                <!--MinWidth="{Binding ItemWidth,
                     RelativeSource={RelativeSource Self}}"-->
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>

        <ListView.GroupStyle>
            <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}" />
        </ListView.GroupStyle>


    </ListView>

有任何想法吗?
我正在尝试插入一些合适的 Setter在为 GroupItem 定义的样式中,但我开始认为这不是正确的做法。

谢谢!

最佳答案

经过多次尝试,我终于找到了要编辑的正确属性。

我想如果有人需要做一些具有相同行为的事情,在这里发布它可能会很有用:

所以我们实际上有一个属性 PanelGroupStyle我们可以在其中添加这个非常需要的 WrapPanel :

<ListView.GroupStyle>
    <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
        <GroupStyle.Panel>
            <ItemsPanelTemplate>
                <WrapPanel Width="800" />
            </ItemsPanelTemplate>
        </GroupStyle.Panel>
    </GroupStyle>
</ListView.GroupStyle>

关于wpf - 使用 WrapPanel 样式化 ListView.GroupStyle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5713224/

相关文章:

wpf - clickonce 部署后出现 xamlparser 错误。安装后应用程序崩溃

javascript - React Native ListView 项目不显示

android - android中列表的listview

wpf - 在 UserControl 中定义 KeyBindings,但它们是否适用于整个窗口?

c# - 如何在 WebBrowser 实例中在 WPF 和 JavaScript 之间进行通信?

wpf - wpf 中包含静态和动态数据的组合框

wpf - 使用混合编辑单独资源文件中的样式

c# - 如何避免在 XAML/Mvvm/C# 中双击按钮

android - Recycler View 列表适配器更新 UI 不正确

c# - ItemsControl with WrapPanel 作为 ItemsPanel - 组合一个 "static"子项和 ItemsSource