自定义ItemsControl与可选的分页

时间:2015-01-15 15:17:01

标签: c# wpf wpf-controls

我有一个非常特殊的要求; - )

我想开发一个带有“上一个”控件和“下一个”控件的ItemsControl。像这样绑定到任意ViewModel:

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

在示例中,我传递了由ViewModel命令控制的2个按钮。如果有人可以告诉我如何收听Control.IsEnable状态并将PreviousControl显示为第一项(如果启用)并将NextControl显示为最后一项(如果启用),那将是非常棒的。

Example Paged ItemsControl

谢谢

1 个答案:

答案 0 :(得分: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>

希望这有帮助!