find index of StackPanel in dynamically filled Listview in WPF

425 views Asked by At

I am trying to find the index of the StackPanel in an ListView which is filled with an binding to public ObservableCollection<Artikel> Artikels { get; set; } = new ObservableCollection<Artikel>();.

<Window x:Class="KassaBlokboek.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:KassaBlokboek"
        mc:Ignorable="d"
         WindowState="Maximized" ResizeMode="NoResize"
        WindowStyle="None" WindowStartupLocation="CenterScreen"
        Title="MainWindow" Height="350" Width="525"
        MinWidth="768"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="7*"/>
        </Grid.RowDefinitions>
        <Image Source="Resources/LogoNew.jpg" Grid.Row="0" VerticalAlignment="Top"  HorizontalAlignment="Left"></Image>
        <TextBox PreviewTextInput="TextBox_OnPreviewTextInput" Name="TextBox" KeyDown="ProductScanned" HorizontalAlignment="Center" Grid.Row="0" Width="768" Height="64" VerticalAlignment="Bottom" Margin="0,-25,0,5"></TextBox>

        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="5*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>

            <Button Click="ScrollUp" x:Name="HoofdButton" Grid.Row="0" Grid.Column="0">Product naar boven</Button>
            <ListView ScrollViewer.VerticalScrollBarVisibility="Hidden" x:Name="listView" Grid.Row="0" Grid.RowSpan="5" Grid.Column="1"
                      ItemsSource="{Binding Artikels}">
                <ListView.ItemTemplate>
                    <DataTemplate DataType="local:Artikel">
                        <Grid Height="{Binding ElementName=HoofdButton, Path=ActualHeight}" HorizontalAlignment="Stretch">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="2*"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                                <ColumnDefinition Width="*"></ColumnDefinition>
                            </Grid.ColumnDefinitions>
                            <StackPanel Grid.Column="0">
                                <TextBlock Text="{Binding Naam}"></TextBlock>
                                <TextBlock Text="{Binding Prijs, StringFormat=€0.00}"></TextBlock>
                                <TextBlock Text="{Binding Barcode}"></TextBlock>
                                <TextBlock Text="{Binding Aantal}"></TextBlock>
                            </StackPanel>
                            <Button Click="Verwijder"  Grid.Column="1">Verwijder</Button>
                            <Button Click="WijzigAantal" Grid.Column="2" Margin="25,0,0,0">Aantal</Button>
                            <Button Click="VerhoogAantal" Grid.Column="3" Margin="25,0,0,0">+</Button>
                            <Button Click="VerlaagAantal" Grid.Column="4" Margin="25,0,0,0">-</Button>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListView.ItemContainerStyle>
            </ListView>

            <Button Click="ScrollDown" Grid.Row="4" Grid.Column="0">Product naar beneden</Button>
            <StackPanel Grid.Row="5" Grid.Column="1" HorizontalAlignment="Right" Orientation="Horizontal">
                <Button Click="BetaalContant" Width="128">Contant</Button>
                <Button Width="128" Margin="25,0,0,0" Click="BetaalPinnen">Pinnen</Button>
                <Button Width="128" Margin="25,0,0,0" Click="Retour">Retour</Button>
            </StackPanel>
            <TextBlock x:Name="totaalPrijs" Margin="10,9.8,0,0" Grid.Row="5" Grid.Column="0" Text="Totaalprijs: €0.00"/>
        </Grid>
    </Grid>
</Window>

I need to find the index of the StackPanel, so i can edit the text in the "Aantal" TextBlock in the StackPanel.

0

There are 0 answers