Wpf ListView:在资源字典中定义ItemsPanelTemplate
我有一个ListView,它的布局看起来像Windows资源管理器视图(图标+一些详细信息),绑定到ViewModel中某个位置的列表 我的目标是能够随时在explorer视图或classic视图之间切换 我可以定义一个Wpf ListView:在资源字典中定义ItemsPanelTemplate,wpf,listview,mvvm,datatemplate,itemspanel,Wpf,Listview,Mvvm,Datatemplate,Itemspanel,我有一个ListView,它的布局看起来像Windows资源管理器视图(图标+一些详细信息),绑定到ViewModel中某个位置的列表 我的目标是能够随时在explorer视图或classic视图之间切换 我可以定义一个ItemsPanelTemplate,直接在ListView.ItemsPanel字段中正确显示布局。现在,我想在参考资料中定义它,这样我就可以在不同的视图中使用它,尤其是在一个控件中,用户应该可以在浏览器视图或经典列表视图(列表的默认呈现)之间进行选择 你是怎么做到的?我无法在
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使用一种样式。所以你会这样做:
<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>
如果希望用户能够在explorer和classic视图之间切换,只需定义第二种样式并切换listview的样式。这可以通过一些VisualState和“DataStateBehavior”来实现
或者,您可以为单个项目创建一个带有一些DataTrigger和setter的样式span。太棒了,谢谢,真不敢相信我还没有考虑过为整个ListView设置样式。我想我会看看你的行为。再次感谢其他相关问题:我不知道如何以预定义的样式定义
ListView.GroupStyle
。它应该是只读属性,我不知道如何定义它。有什么想法吗?