2011-04-18 98 views
8

我有一个ListView,其布局看起来像一个Windows资源管理器视图(图标+一些细节),绑定到ViewModel某处的列表。ListView:在资源字典中定义ItemsPanelTemplate

我的目标是能够随时在资源管理器视图或经典视图之间切换。

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

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

的实际列表的代码片段:

<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"> 

       <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> 

      </ListView> 

在静态资源保持ItemTemplate是容易做到的,但我现在不能做的事情ItemsPanelTemplate ...

任何想法?我正在使用MVVM,所以我尽量不要使用代码隐藏

回答

7

你会为整个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> 

,如果你想那么用户能够和探险家和经典视图之间切换,只需确定第二风格切换列表视图的风格。这可以通过一些VisualStates和一个'DataStateBehavior'完成。

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

+0

真棒,谢谢,不敢相信我没有想过造型整个ListView。猜猜我会看看DataStateBehavior。再次感谢 – Damascus 2011-04-19 06:45:44

+0

另一个相关的问题:我不知道如何以预定义的风格定义'ListView.GroupStyle'。它应该是只读属性,我不知道如何定义它。任何想法? – Damascus 2011-04-19 15:07:21