Question

To simulate a modal dialog in WPF, I display a Window and call: Mouse.Capture(dialogBoxArea, CaptureMode.SubTree);

The call returns false.

Mouse.Captured is null. dialogBoxArea.Visibility is Visibility.Visible. dialogBoxArea.IsEnabled is true.

If the line is called again a second time, it returns true and correctly captures the mouse.

What condition might I be missing that is preventing the capture from working?

Edit

Here's what I've tried so far.

        if (Mouse.Captured != null)
        {
            // Not called, so presumably, nothing has already captured the mouse
            MessageBox.Show("already captured");
        }

        if (dialogBoxArea.Visibility != Visibility.Visible)
        {
            // Not called
            MessageBox.Show("not visible");
        }

        if (!dialogBoxArea.IsEnabled)
        {
            // Not called
            MessageBox.Show("not enabled");
        }

        // According to documentation, this should release mouse capture from anything that holds it
        Mouse.Capture(null);

        // Attempt to capture the mouse
        if (!Mouse.Capture(dialogBox, CaptureMode.SubTree))
        {
            // This is called
            Mouse.Capture(null);
            Mouse.Capture(dialogBox, CaptureMode.SubTree);
        }
Was it helpful?

Solution

As a first iteration i would talk to your client.

The following opens a dialog option window that is always on top of the original window and blocks calls to it, but does not hinder the overall execution at all. If your customer sees the behaviour he may be happy with that.

namespace StackoverflowExample
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }
    void NewWindowAsDialog(object sender, RoutedEventArgs e)
    {
      Window myOwnedDialog = new Window();
      myOwnedDialog.Owner = this;
      myOwnedDialog.ShowDialog();
    }
  }
}

I will post another option later here that will illustrate how to load a window into a subdivision (grid etc.) of your xaml. You could filter all other calls based on the content that is loaded into that division rather then filtering the mouscall. Your filtering could run into the problem of the logical vs the viewtree - you only ever want to look at the trees if you create your own templates from scratch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top