如何将不同结构的项目插入ListView?

时间:2016-05-24 08:11:38

标签: wpf xaml

我有一个ListView,它通过包含一个包含图像的网格和一个按钮的项目进行动态填充:

<ListView  x:Name="Thumbnails" HorizontalContentAlignment ="Left" VerticalContentAlignment="Top" Padding="0"  Background ="#81AFD3" Grid.Row="6" Grid.Column="6" Grid.ColumnSpan="10" Grid.RowSpan="27" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemsSource="{Binding ThumbModelList}" SelectedIndex="{Binding SelectedIndx}">
  <ListView.ItemTemplate>
                    <DataTemplate>
                        <ListViewItem>

                            <Grid Width="{Binding DataContext.ThumbnailWidth, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" Height="{Binding DataContext.ThumbnailHeight, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" HorizontalAlignment="Center" VerticalAlignment="Top" Background="Transparent" >

                                <Grid.RowDefinitions>
                                    <RowDefinition Height="1*" />
                                    <RowDefinition Height="1*" />
                                    <RowDefinition Height="1*" />
                                    <RowDefinition Height="1*" />
                                    <RowDefinition Height="1*" />
                                    <RowDefinition Height="1*" />
                                    <RowDefinition Height="1*" />
                                </Grid.RowDefinitions>

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                    <ColumnDefinition Width="1*" />
                                </Grid.ColumnDefinitions>

                                <Image Name="thumbImage" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="14" Grid.RowSpan="7" Stretch="UniformToFill" Source="{Binding MainThumbImagePath}"></Image>

                                 <Button Name="slide_ON_OFF" Grid.Row="0" Grid.Column="10" Grid.ColumnSpan="4" Grid.RowSpan="4" VerticalAlignment="Center" Background="Transparent" Command="{Binding DataContext.SlideOnOffCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}">
                                    <Image Source="{Binding SomePath}"/>
                                </Button>

                            </Grid>
                        </ListViewItem>
                    </DataTemplate>
                </ListView.ItemTemplate>

            </ListView>


        now, once the list is populated dynamically with those items, at certain positions, I want to insert a diffrerent item that has three different buttons like this:

     <Grid Width="{Binding DataContext.ThumbnailWidth, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" Height="{Binding DataContext.ThumbnailHeight, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}" HorizontalAlignment="Center" VerticalAlignment="Top" Background="Transparent" >

                        <Grid.RowDefinitions>
                            <RowDefinition Height="1*" />
                            <RowDefinition Height="1*" />
                            <RowDefinition Height="1*" />
                            <RowDefinition Height="1*" />
                            <RowDefinition Height="1*" />
                            <RowDefinition Height="1*" />
                            <RowDefinition Height="1*" />
                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="1*" />
                        </Grid.ColumnDefinitions>


                         <Button Name="slide_ON_OFF" Grid.Row="0" Grid.Column="10" Grid.ColumnSpan="4" Grid.RowSpan="4" VerticalAlignment="Center" Background="Transparent" Command="{Binding DataContext.Command1, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}"/>
                         <Button Name="slide_ON_OFF" Grid.Row="0" Grid.Column="10" Grid.ColumnSpan="4" Grid.RowSpan="4" VerticalAlignment="Center" Background="Transparent" Command="{Binding DataContext.Command2, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}"/>
                         <Button Name="slide_ON_OFF" Grid.Row="0" Grid.Column="10" Grid.ColumnSpan="4" Grid.RowSpan="4" VerticalAlignment="Center" Background="Transparent" Command="{Binding DataContext.Command3, RelativeSource={RelativeSource FindAncestor, AncestorType=ListView}}"/>
     </Grid> 

是否可以这样做,以及在XAML中声明此其他列表项的位置和方式?

1 个答案:

答案 0 :(得分:1)

您可以通过数据类型或模板选择器使用datatemplates,以下是数据类型的示例:Example

这是一个简单的, 它还取决于您的数据/ Datacontext