首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ListView:在资源字典中定义ItemsPanelTemplate

ListView:在资源字典中定义ItemsPanelTemplate
EN

Stack Overflow用户
提问于 2011-04-19 00:03:21
回答 1查看 12.3K关注 0票数 10

我有一个ListView,它的布局看起来像一个Windows Explorer视图(图标+一些细节),绑定到ViewModel中的某个地方的列表。

我在这里的目标是能够在资源管理器视图或经典视图之间进行切换。

我可以直接在ListView.ItemsPanel字段中定义一个执行正确显示布局工作的ItemsPanelTemplate。现在,我想在参考资料中定义它,这样我就可以在不同的视图中使用它,特别是在一个控件中,用户应该可以在资源管理器视图或经典列表视图(列表的默认呈现)之间进行选择。

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

实际列表的代码片段:

代码语言:javascript
复制
<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,所以如果可能的话,我尽量不使用代码隐藏

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-19 01:17:27

为此,您将为整个ListView使用样式。所以你会这样做:

代码语言:javascript
复制
<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>

如果您希望用户能够在资源管理器和经典视图之间切换,只需定义另一个样式并切换列表视图的样式即可。例如,这可以通过一些VisualStates和一个“DataStateBehavior”来完成。

或者,您可以为单个ItemsPanels创建一个带有一些DataTriggers和Setters的样式。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5705666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档