0

after a quite bit of googling I haven't found answer on this problem. I have this ListView

<ListView x:Name="itemListView" ItemsSource="{Binding History}"  HorizontalAlignment="Stretch">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment"  Value="Stretch"></Setter>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Border Style="{StaticResource Border}" Background="{ThemeResource GradientLinear}" HorizontalAlignment="Stretch">
                            <Grid>
                                <StackPanel Orientation="Vertical" Margin="10,10,10,10" HorizontalAlignment="Left" Grid.Row="0" Grid.Column="0">
                                    <TextBlock Text="{Binding name}" />
                                    <TextBlock Text="{Binding question}" TextWrapping="Wrap"/>
                                    <TextBlock Text="{Binding vrijeme}"/>
                                </StackPanel>
                                <Button Width="50" Height="50" Opacity="0.6" Grid.Row="0" Grid.Column="1" Tag="{Binding name}"
                                        Command="{Binding ElementName=itemListView, Path=DataContext.Delete}" CommandParameter="{Binding ElementName=itemListView, Path=SelectedIndex}"
                                        HorizontalAlignment="Right">
                                    <SymbolIcon 
                                        HorizontalAlignment="Center"
                                        Width="48"
                                        Height="48"
                                        Symbol="Delete" />
                                </Button>
                            </Grid>
                        </Border>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

Problem is probably in this part of the code

<Button Width="50" Height="50" Opacity="0.6" Grid.Row="0" Grid.Column="1" Tag="{Binding name}"
                                        Command="{Binding ElementName=itemListView, Path=DataContext.Delete}" CommandParameter="{Binding ElementName=itemListView, Path=SelectedIndex}"
                                        HorizontalAlignment="Right">

When I click Button, ListViewItem that contains that Button is not selected so I get -1 for SelectedIndex or just null if I target SelectedItem. It seems that clicking that button leaves containing ListViewItem unaffected. If I first click ListViewItem and than click button it works, SelectedItem is set in ListView of course, but I can't count user will understand that. My question is - is there any way to "force" Button to send me in CommandArgument SelectedItem or SelectedIndex. I know how to do that through code-behind but I am interested in solving this problem through MVVM. Thanx in advance.

Ogmi
  • 257
  • 2
  • 6

0 Answers0