Richard Morgan

I've got an application that uses several modal dialog windows. Early on, I discovered that displaying the window was a little slow, apparently because the XAML had to be parsed each time the window was displayed. In order to circumvent this delay, I create each of the modal dialog windows at the beginning of my application, and I never call Close() on them (nor do I set the DialogResult property, which would also close the window). I do make a call to ShowDialog(), and the window functions properly as a modal dialog. When I'm done, I call the Hide() method, which returns from the modal ShowDialog() call and allows me to continue processing.

After a while, the windows start exhibiting some strange behavior. Sometimes the wrong window will display. Usually the correct window displays, but the buttons and controls on the window do not appear to respond. I say "do not appear" because in some cases the buttons' click handlers will fire, but the button never displays in a pressed state. And sometimes, what actually fires is a button handler for a different button on a different window (but always a window that I have either used or will use in a modal capacity).

Has anyone seen any behavior that resembles this Any ideas on how to resolve it I know I can go back to creating the window each time before I display, but I'm reluctant to do this because I'd like the display to be quick.



Re: Windows Presentation Foundation (WPF) ShowDialog() behavior

Drew Marsh

This pattern works for me flawlessly:

<Window x:Class="Test.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Test" Height="300" Width="300">
<
Grid>

</
Grid>
</
Window>

public class Window1 : Window
{
private Window2 dialog;

public Window1()
{
InitializeComponent();
}

protected override OnMouseUp(RoutedEventArgs args)
{
base.OnMouseUp(args);

if(this.dialog == null)
{
this.dialog = new Window2();
}

bool result = this.dialog.ShowDialog();
}
}

<Window x:Class="Test.Window2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Test" Height="300" Width="300">
<
Grid>
<
Button Name="button1" IsDefault="True" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="75">Button</Button>
</
Grid>
</
Window>

public class Window2 : Window
{
public Window2()
{
InitializeComponent();

this.button.Click += new RoutedEventHandler(delegate { this.Hide(); });
}
}





Re: Windows Presentation Foundation (WPF) ShowDialog() behavior

Richard Morgan

Drew -

I use an approach very similar to what you're doing; close enough, that I believe we can consider them the same. And most of the time it works flawlessly. I have been unable to figure out what causes the problem to appear. There is a lot going on in my application, and the windows that end up exhibiting the problem all use a listbox with custom template (and custom data templates for the ListBoxItems) that databinds to an ObservableCollection. Sometimes the buttons on the dialog window will actually fire their click handlers - but they no longer display up and down states as they are pressed. Likewise, I often have an editbox that accepts input and has focus - but the flashing text caret is no longer displayed.

I'm wondering if there's something in the way dialog windows are handled internally that is causing my problem. Or perhaps dialog windows were intended to work only with the Close() method and not the Hide() method, and therefore my approach is causing my problem. I haven't yet been able to track down the interplay of all the potential factors involved.





Re: Windows Presentation Foundation (WPF) ShowDialog() behavior

Drew Marsh

Hmmm... I guess I can't really help much more than that then. I can click that thing til the cows come home and not have a problem. The first time it loads the window takes longer as expected, but from that point on it's pretty much instantaneous. FWIW, I'm on Vista (RC2 even).

-Drew





Re: Windows Presentation Foundation (WPF) ShowDialog() behavior

Richard Morgan

I'm running on Windows XP, with the released version of .NET 3.0. When the problem occurs, it always involves *two* modal windows. In other words, if I have a window that exhibits the problem, another window will have the problem as well. I've done enough research to know that the underlying workings of the windows are correct - buttons, even though they no longer visually depress, will fire the correct click handler. I previously thought that somehow the fired events got messed up, but I was wrong.

So what really happens is that two modal windows no longer update their visual display. The buttons still respond to mouse clicks, and any edit boxes still accept input (even though you cannot visually see the input). It is not always the same two windows that have the problem; the actual windows having the visual display problem will change.

I'll keep digging into it (I have no choice; I've got a deadline!)

Thanks for your input.





Re: Windows Presentation Foundation (WPF) ShowDialog() behavior

SerialSeb

It might sound stupid, but I chased a bug in my code once for a few hours where nothing was happening anymore and the content of my element wasn't reacting anymore... And as it happens I had forgotten to call the base.OnXX in one of the overrides in my class... Double check them just in case :)




Re: Windows Presentation Foundation (WPF) ShowDialog() behavior

Richard Morgan

I have chased this down far enough to know that it does not happen if I do not use windows that support transparency. I have changed the windows that I use so that they no longer support transparency, and the problem is gone.