【问题标题】:How to insert items of different structure into the ListView?如何将不同结构的项目插入ListView?
【发布时间】:2016-05-24 16:19:42
【问题描述】:

我有一个 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 中声明这个其他列表项?

【问题讨论】:

  • 您应该为每种数据类型使用数据模板
  • 好的,但是它如何知道最初要填充的数据类型呢?我首先希望通过“DataType1”循环填充列表,然后在某些用户操作上手动插入“Datatype2”。
  • ThumbModelList 类型应该是 itemtype1 和 itemtype2 的基本类型列表(例如 ObserbableCollection),现在当您添加 type1 或 type2 项目时,它具有相应的数据模板,如我在答案中的示例下面

标签: wpf xaml


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多