ListView:在资源字典中定义ItemsPanelTemplate

10

我有一个ListView,其布局类似于Windows资源管理器视图(图标+一些细节),绑定到ViewModel中的某个列表。

我的目的是能够在需要时在浏览器视图和经典视图之间切换。

我可以定义一个ItemsPanelTemplate来直接在ListView.ItemsPanel字段中正确显示布局。现在,我想在资源中定义它,以便我可以在不同的视图中使用它,尤其是在一个控件中,用户应该可以选择浏览器视图或经典列表视图(列表的默认呈现方式)。

你会怎么做?我不能在我的ResourceDictionary中定义任何ItemsPanelTemplate,如果我定义一个DataTemplate,它就不兼容(虽然我认为按照纯逻辑,ItemsPanelTemplate应该继承自DataTemplate,但实际上并不像这样)。

实际列表的代码片段:

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel
            Width="{Binding (FrameworkElement.ActualWidth),
                RelativeSource={RelativeSource 
                AncestorType=ScrollContentPresenter}}"
            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.ItemTemplate>
    <DataTemplate>
        <StackPanel
            Orientation="Horizontal" 
            Height="Auto" 
            Width="150" >
            <Image 
                Source="{Binding Path=Appli.AppType, Converter={StaticResource TypeToIconConverter}}"
                Margin="5"
                Height="50"
                Width="50" />
            <StackPanel 
                VerticalAlignment="Center"
                Width="90" >
                <TextBlock 
                    Text="{Binding Path=Appli.AppName}" 
                    FontSize="13" 
                    HorizontalAlignment="Left" 
                    TextWrapping="WrapWithOverflow"
                    Margin="0,0,0,1" />
                <TextBlock 
                    Text="{Binding Path=Appli.AppType}" 
                    FontSize="9" 
                    HorizontalAlignment="Left" 
                    Margin="0,0,0,1" />
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>

ItemTemplate保留为静态资源很容易做到,但现在我无法对ItemsPanelTemplate进行任何操作...

有什么想法吗?我正在使用MVVM,因此如果可能的话,理想情况下尽量不使用代码后台。

1个回答

10

你需要为整个ListView使用一个样式。因此,你应该这样做:

<Grid.Resources>
    <Style x:Key="ListViewStyle" TargetType="ListView">
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel 
                        Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}"
                        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>
            </Setter.Value>
        </Setter>
    </Style>
</Grid.Resources>

<ListView
    SelectionMode="Single"
    VerticalAlignment="Stretch"
    HorizontalAlignment="Stretch"
    HorizontalContentAlignment="Center"
    VerticalContentAlignment="Bottom"
    ScrollViewer.VerticalScrollBarVisibility="Auto"
    ItemsSource="{Binding ListUserApps, UpdateSourceTrigger=PropertyChanged}"
    SelectedIndex="{Binding SelectedUserApp, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    Background="White"
    Style="{StaticResource ListViewStyle}">

    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel
                Orientation="Horizontal"
                Height="Auto"
                Width="150">
                <Image
                    Source="{Binding Path=Appli.AppType, Converter={StaticResource TypeToIconConverter}}"
                    Margin="5"
                    Height="50"
                    Width="50"/>
                <StackPanel
                    VerticalAlignment="Center"
                    Width="90">

                    <TextBlock
                        Text="{Binding Path=Appli.AppName}"
                        FontSize="13"
                        HorizontalAlignment="Left"
                        TextWrapping="WrapWithOverflow"
                        Margin="0,0,0,1" />
                    <TextBlock
                        Text="{Binding Path=Appli.AppType}"
                        FontSize="9"
                        HorizontalAlignment="Left"
                        Margin="0,0,0,1" />

                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>

</ListView>
如果你想让用户能够在资源管理器和经典视图之间切换,只需定义第二个样式并切换列表视图的样式。这可以使用一些可视状态和“DataStateBehavior”实现。或者,您可以创建一个样式,并为每个项面板设置一些数据触发器和Setters。

太棒了,谢谢,我简直不敢相信我没想过为整个ListView设置样式。那我就看看DataStateBehavior吧。再次感谢。 - Damascus
另一个相关的问题:我无法弄清如何在预定义的样式中定义 ListView.GroupStyle。 它应该是一个只读属性,我不知道该如何定义它。 有什么想法吗? - Damascus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接