Raymond Osterbrink
Raymond Osterbrink

Reputation: 540

How do I add a custom item to a ListView with a Collection binding

i've got a ListView with item-binding which looks like this:

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

what i'd like to do, is adding a topmost item, which is either unbound, nor uses the Datatemplate, sth. like this:

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

Is this somehow possible, without having to change my specified Collection to ObservableCollection for adding the Item and then using a TemplateSelector? I was hoping to avoid this, since I have the feeling this would be a bit of an overkill for only one single Item.

Upvotes: 0

Views: 124

Answers (1)

thang2410199
thang2410199

Reputation: 1942

Use Header and put your content into ListView.HeaderTemplate. Since you only want to put 1 item ontop, no need to change your collection.

Upvotes: 1

Related Questions