There is a table:

<DataGrid x:Name="dataGrid" AutoGenerateColumns="False" Margin="0,449,0,0" IsReadOnly="True"> <DataGrid.Columns> <DataGridTextColumn Header="НомСр ПК" Binding="{Binding Path=Id}"/> <DataGridTextColumn Header="ΠŸΡ€Ρ–Π·Π²ΠΈΡ‰Π΅" Binding="{Binding Path=First_name}"/> ... </DataGrid.Columns> <DataGrid.RowDetailsTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Content="Π²ΠΈΠ΄Π°Π»ΠΈΡ‚ΠΈ" Click="Delete_Click"/> <Button Content="Π²Ρ–Π΄ΠΊΡ€ΠΈΡ‚ΠΈ ΠΏΠΊ"/> </StackPanel> </DataTemplate> </DataGrid.RowDetailsTemplate> </DataGrid> 

How to get column value ( <DataGridTextColumn Header="НомСр ПК" Binding="{Binding Path=Id}"/> ) in the code, then to delete the record by ID? Help pliz!

  • S-Sharpik ........ - Awesome7997
  • yes all the same, the main thing - not much powdered ... - Awesome7997
  • Is your question resolved? - Ev_Hyper
  • @Ev_Hyper, solved the problem with a govnokod, depressing ... - Awesome7997
  • I wrote the answer ... about the database I can not say anything, because you did not give any information about it. - Ev_Hyper

1 answer 1

In such cases, you can use the command (as already noted in the comments):

 <DataGrid.RowDetailsTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Content="Π²ΠΈΠ΄Π°Π»ΠΈΡ‚ΠΈ" Command="{Binding DataContext.RemoveCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding}" /> <Button Content="Π²Ρ–Π΄ΠΊΡ€ΠΈΡ‚ΠΈ ΠΏΠΊ" /> </StackPanel> </DataTemplate> </DataGrid.RowDetailsTemplate> 

In your ViewModel, define the corresponding RemoveCommand command:

 public ObservableCollection<Info> Data { get; set; } public void Remove(object param) { Info select = param as Info; Data.Remove(select); } public ICommand RemoveCommand { get; set; } public ViewModel() { RemoveCommand = new RelayCommand(Remove); Data = new ObservableCollection<Info>(GetData()); } 

Instead of Info , naturally, your class should be.