我有一个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,因此如果可能的话,理想情况下尽量不使用代码后台。
ListView.GroupStyle
。 它应该是一个只读属性,我不知道该如何定义它。 有什么想法吗? - Damascus