Hello! There is a button created inside the ListView through the Template:

<GridViewColumn Width="80" Header="Скачать"> <GridViewColumn.CellTemplate> <DataTemplate> <Button Name="DownloadBtn" Click="Button_Click" Content="Скачать" /> </DataTemplate> </GridViewColumn.CellTemplate> </GridViewColumn> 

It is necessary to make it so that when you click on it, the entire ListView row is selected.

In general, the program should download an item by reference from the collection bound to the ListView, and for this I need to get SelectedIndex from the ListView (or in some other way?).

Just in case, the code from ButtonClick:

  private void Button_Click(object sender, RoutedEventArgs e) { using (var client = new System.Net.WebClient()) { client.DownloadFileAsync( MainViewModel.ModelMainWindow.SongsPlaylist[ MainViewModel.ModelMainWindow.OneClickIndex].SongUri, "D:\\" + MainViewModel.ModelMainWindow.SongsPlaylist[ MainViewModel.ModelMainWindow.OneClickIndex].Artist + " - " + MainViewModel.ModelMainWindow.SongsPlaylist[ MainViewModel.ModelMainWindow.OneClickIndex].Title + ".mp3"); } } 
  • one
    DataContext of Button have a collection item like? (sender as Button).DataContext - Dmitry Chistik
  • Did not know, thanks! - Ep1demic

1 answer 1

I decided to add this section of code to the ListView element:

 <ListView.Resources> <Style TargetType="ListViewItem"> <Style.Triggers> <Trigger Property="IsKeyboardFocusWithin" Value="true"> <Setter Property="IsSelected" Value="True" /> </Trigger> </Style.Triggers> </Style> </ListView.Resources> 

And the lines:

 MainViewModel.ModelMainWindow.OneClickIndex = All_playlist.SelectedIndex; 

in the buttonclick method.

  • one
    There are also SelectedItem / Index / Path controllers collections. - vitidev