如何使用Collection绑定将自定义项添加到ListView

时间:2016-04-08 09:16:05

标签: c# xaml listview win-universal-app

我有一个带有项目绑定的ListView,如下所示:

<ListView x:Name="DetailsAZList" ItemsSource="{Binding AZEntries.AZEntries, Mode=OneWay}" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Background="LightGray" BorderBrush="DimGray" BorderThickness="2" CornerRadius="0" Margin="0 2 0 10">
                <Grid MinHeight="140" HorizontalAlignment="Stretch">
                    <StackPanel Margin="10 0" Orientation="Vertical">
                        <TextBlock Text="{Binding ToDoBezeichnung, Mode=OneWay}"/>
                        <!--UserName Unter umständen auch anzeigen, falls Arbeitszeiten anderer User mit aufgeführt werden-->
                        <!--<TextBlock Text="{Binding UserName, Mode=OneWay}"/>-->
                        <TextBlock Text="{Binding start, Mode=OneWay}"/>
                        <TextBlock Text="{Binding finished, Mode=OneWay}"/>
                        <TextBlock Text="{Binding Kostenpflichtig, Mode=OneWay}"/>
                        <controls:WrapPanel Orientation="Horizontal" BlockSize="40">
                            <!-- Example from: stackoverflow.com/questions/28223693/how-to-bind-buttons-in-listview-datatemplate-to-commands-in-viewmodel-mvvmligh -->
                            <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.PlayCommand}" 
                                    CommandParameter="{Binding}" 
                                    Margin="0 0 10 0">
                                <SymbolIcon Symbol="Play"/>
                            </Button>
                            <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.PauseCommand}" 
                                    CommandParameter="{Binding}" 
                                    Margin="0 0 10 0">
                                <SymbolIcon Symbol="Pause"/>
                            </Button>
                            <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.StopCommand}" 
                                    CommandParameter="{Binding}" 
                                    Margin="0 0 10 0">
                                <SymbolIcon Symbol="Stop"/>
                            </Button>
                            <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.EditCommand}" 
                                    CommandParameter="{Binding}" 
                                    Margin="0 0 10 0">
                                <SymbolIcon Symbol="Edit"/>
                            </Button>
                            <Button Command="{Binding ElementName=DetailsAZList, Path=DataContext.DeleteCommand}" 
                                    CommandParameter="{Binding}" >
                                <SymbolIcon Symbol="Delete"/>
                            </Button>
                        </controls:WrapPanel>
                    </StackPanel>
                </Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Margin" Value="0 2 0 0"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

我想做的是添加一个最上面的项目,它是未绑定的,也不是使用Datatemplate,某事。像这样:

<ListView x:Name="DetailsAZList" ItemsSource="{Binding AZEntries.AZEntries, Mode=OneWay}" >

    <!-- Here comes the relevant Item -->
    <ListViewItem x:Name="TopmostUnboundItem">
        <Grid HorizontalAlignment="Strech" Background="LightGray">
            <TextBox Text="Add Entry"/>
        </Grid>
    </ListViewItem>
    <!-- Thats it -->

    <ListView.ItemTemplate>
        <DataTemplate>
            <Border Background="LightGray" BorderBrush="DimGray" BorderThickness="2" CornerRadius="0" Margin="0 2 0 10">
                <Grid>...</Grid>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="Margin" Value="0 2 0 0"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

这是否可行,无需将我指定的Collection更改为ObservableCollection以添加Item然后使用TemplateSelector?我本来希望避免这种情况,因为我觉得这对于一件物品来说有点过分。

1 个答案:

答案 0 :(得分:1)

使用Header并将您的内容放入ListView.HeaderTemplate。由于您只想在上面放置1个项目,因此无需更改您的收藏。