2015-01-15 129 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