【问题标题】:Styling ListView.GroupStyle with a WrapPanel使用 WrapPanel 样式化 ListView.GroupStyle
【发布时间】:2011-04-19 07:51:14
【问题描述】:

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

我有一个 ListView,我对其进行了样式设置,使其看起来像 Windows 资源管理器。

现在,我想对里面的项目进行分组。因此,我定义了一个GroupStyle 和一个Expander 来对它进行分组。分组现在很好。

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

我猜图片比文字好。

这是我所拥有的:

这就是我想要的:

我找不到应该设置哪个属性才能使 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>

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

谢谢!

【问题讨论】:

  • +1 哈哈!复选框

标签: wpf xaml listview styles controltemplate


【解决方案1】:

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

如果有人需要以相同的行为做某事,我想在此处发布它可能会很有用:

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

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

【讨论】:

  • 在 .NET 4.5 中不再需要添加 GroupStyle.Panel,只需设置 ListBox.ItemsPanel 就足够了。如果您设计了运行 .NET 4.5 的应用程序,然后在只运行 4.0 的客户端上看到一切都搞砸了,这可能会非常令人沮丧。 MS 在 4.5 就地升级中添加了相当多的陷阱......
【解决方案2】:

如果有人在这里,就像我正在尝试制作 ListBox Items Wrap 但基于未知数量的项目,因此您无法像上面的答案那样设置 Width,这就是我的做法。

<ListBox.ItemsPanel>
<ItemsPanelTemplate>
    <WrapPanel Orientation="Vertical" MaxHeight="{Binding Converter={StaticResource ListBoxHeightToItemsPanelHeightConverter}, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}, Path=ActualHeight}"/> 
</ItemsPanelTemplate>
</ListBox.ItemsPanel>

在我的转换器中,我只需减去 30 来计算标题的高度。

完整代码如下:

<ListBox.GroupStyle>
<GroupStyle>
    <GroupStyle.HeaderTemplate>
        <DataTemplate>
            <TextBlock Margin="8" FontSize="18" TextAlignment="Center" FontWeight="Bold" Foreground="White" >
                <TextBlock.Text>
                    <Binding Path="Name"/>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </GroupStyle.HeaderTemplate>
    <GroupStyle.Panel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </GroupStyle.Panel>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
    <WrapPanel Orientation="Vertical" MaxHeight="{Binding Converter={StaticResource ListBoxHeightToGroupStyleHeightConverter}, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBox}}, Path=ActualHeight}"/> 
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.Template>
<ControlTemplate>
    <!-- Your template here. -->
</ControlTemplate>
</ListBox.Template>
<ListBox.ItemTemplate>
<DataTemplate >
    <!-- Your template here. -->
</DataTemplate>
</ListBox.ItemTemplate>

希望这有助于节省一些时间!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    相关资源
    最近更新 更多