1

I have created a custom XAML UserControl class that I pass to the ShowDialog method. I would like to be able to trigger clicking the OK button on the dialog through other events generated in my UserControl - for example double clicking a ListItem. I have the code to handle the double click just fine (tied into the MouseDown even and checked the click count) but I don't know how to trigger a new event to the parent dialog to close it.

// Bound to TextBlock, part of a ListBox on a UserControl
private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount == 2)
    {
        // trigger dialog to close with OK button
    }
}
Charles
  • 50,943
  • 13
  • 104
  • 142
Goyuix
  • 23,614
  • 14
  • 84
  • 128

1 Answers1

2

Make your user control implement IDialogContent. You can then call directly to the CloseDialog event you implemented, and that will trigger the dialog closure.

You will need to decide how you want to handle that in your follow up code by setting some sort of state on your user control/view model or some other data as fits your particular extension.

// Bound to TextBlock, part of a ListBox on a UserControl
    private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if (e.ClickCount == 2)
        {
            CloseDialog(this, EventArgs.Empty);
        }
    }
Yishai Galatzer
  • 8,791
  • 2
  • 32
  • 41