我有一个 ListView,它的布局看起来像一个 Windows 资源管理器视图(图标 + 一些细节),绑定到 ViewModel 中某处的列表。
我的目标是能够随时在资源管理器视图或经典视图之间切换。
我可以ItemsPanelTemplate
直接在现场定义一个完全正确地显示布局的工作ListView.ItemsPanel
。现在,我想在资源中定义它,这样我就可以在不同的视图中使用它,尤其是在一个控件中,用户应该可以在资源管理器视图或经典列表视图之间进行选择(默认呈现一个列表)
你是怎么做到的?我不能ItemsPanelTemplate
在 my中定义任何内容ResourceDictionary
,如果我定义 aDataTemplate
它不兼容(虽然我认为遵循纯逻辑,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,所以我尽量不使用代码隐藏