2015-01-15 124 views
0

我有一个非常特别的请求;-)自定义ItemsControl与可选寻呼

我想开发一个ItemsControl与“上一个”控制和“下一个”控制。像这样结合到任意的视图模型:

<controls:PagedItemsControl ItemsSource="{Binding Items}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <UniformGrid Columns="5" /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate DataType="system:String"> 
      <Border Background="Gray" Margin="5"> 
       <TextBlock Text="{Binding}" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
      </Border> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    <controls:PagedItemsControl.PreviousControl> 
     <Button Content="Previous" Command="{Binding PreviousCommand}" /> 
    </controls:PagedItemsControl.PreviousControl> 
    <controls:PagedItemsControl.NextControl> 
     <Button Content="Next" Command="{Binding NextCommand}" /> 
    </controls:PagedItemsControl.NextControl> 
</controls:PagedItemsControl> 

在这个例子中我经过视图模型的命令来控制2个按钮。如果有人能告诉我如何聆听Control.IsEnable状态并将PreviousControl显示为第一项(如果启用)并将NextControl作为最后一项(如果启用),那将会非常棒。

Example Paged ItemsControl

谢谢

+0

你需要一个'CollectionView'(可能是一个'CompositeCollection',因为你的Previous和Next元素将会是一个不同于其他项的类型),还有一个'RoutedCommand'来处理前一个/下一个逻辑和启用/禁用。 – 2015-01-15 15:39:51

回答

1

看一看下面的XAML。在使用ItemsSource时,我们不能将元素添加到ItemsPanel。但是我们可能会尝试构建一个棘手的集合,它由ItemsSource和其他元素组成。不幸的是,CollectionContainer无法直接绑定到Items。幸运的是,好人已经为这种情况找到了solution

<Grid> 
    <Grid.Resources> 
     <BooleanToVisibilityConverter x:Key="BoolToVisibilityConverter"/> 
    </Grid.Resources> 
    <TextBlock Name="TrickyBinder" 
       Tag="{Binding Items}" 
       Visibility="Collapsed"/> 
    <ItemsControl> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Columns="5" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate DataType="system:String"> 
       <Border Background="Gray" Margin="5"> 
        <TextBlock Text="{Binding}" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" /> 
       </Border> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     <ItemsControl.ItemsSource> 
      <CompositeCollection> 
       <CollectionContainer> 
        <CollectionContainer.Collection> 
         <col:ArrayList> 
          <Button Content="Previous" Command="{Binding PreviousCommand}" Visibility="{Binding Path=IsEnabled,RelativeSource={RelativeSource Self},Converter={StaticResource BoolToVisibilityConverter}}" /> 
         </col:ArrayList> 
        </CollectionContainer.Collection> 
       </CollectionContainer> 
       <CollectionContainer Collection="{Binding Path=Tag,Source={x:Reference TrickyBinder}}"/> 
       <CollectionContainer> 
        <CollectionContainer.Collection> 
         <col:ArrayList> 
          <Button Content="Next" Command="{Binding NextCommand}" Visibility="{Binding Path=IsEnabled,RelativeSource={RelativeSource Self},Converter={StaticResource BoolToVisibilityConverter}}" /> 
         </col:ArrayList> 
        </CollectionContainer.Collection> 
       </CollectionContainer> 
      </CompositeCollection> 
     </ItemsControl.ItemsSource> 
    </ItemsControl> 
</Grid> 

希望这会有所帮助!

+0

是的,看起来不错。稍作改进: 提供''作为资源和使用'而不是你的“TrickyBinder” – Marcel 2015-01-20 14:32:19

+0

@Marcel同意。我不确定Binding能否正常工作,所以我用了最明显的方式。无论如何,我的例子只是一个概念;) – 2015-01-20 15:01:17