0

I have a DataGrid in my WPF application where every row ends with a Delete button.

 <DataTemplate>
      <Button
       Command="Delete"
       CommandParameter="{Binding}" FontWeight="Bold" Foreground="{x:Null}" IsEnabled="True" BorderBrush="{x:Null}">
      <Button.Background>
      <ImageBrush ImageSource="Ikonok/bin.png"/>
      </Button.Background>
      <Button.Style>
        <Style TargetType="{x:Type Button}">
           <Setter Property="Template">
              <Setter.Value>
                 <ControlTemplate TargetType="{x:Type Button}">
                      <Border Background="{TemplateBinding Background}" BorderBrush="{x:Null}" BorderThickness="1">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                      </Border>
                 </ControlTemplate>
              </Setter.Value>
           </Setter>
        </Style>
     </Button.Style>
     </Button> </DataTemplate>

How can I write a code behind that drops a MessageBox before the delete action that asks if the user really want to delete that item?

Cameleao
  • 63
  • 1
  • 5
  • Here is the `winforms` answer from SO: https://stackoverflow.com/questions/3036829/how-do-i-create-a-message-box-with-yes-no-choices-and-a-dialogresult --You may be able to apply it to what you currently have – Jaskier Jan 29 '19 at 14:25
  • Possible duplicate of https://stackoverflow.com/questions/4199162/ok-cancel-dialog-mvvm-pattern-wpf-how-can-i-do-it/4201604 – Manoj Choudhari Jan 29 '19 at 14:30
  • 1
    Possible duplicate of [Ok cancel Dialog MVVM Pattern wpf.How can I do it](https://stackoverflow.com/questions/4199162/ok-cancel-dialog-mvvm-pattern-wpf-how-can-i-do-it) – Manoj Choudhari Jan 29 '19 at 14:30

1 Answers1

3

When you fire event for the delete button place this code at the beginning of the event.

MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Are you sure?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo);

if (messageBoxResult == MessageBoxResult.Yes)
{
     //...
}    
else
{
    //...
}
ikerbera
  • 195
  • 1
  • 3
  • 15
Dhruv
  • 71
  • 7