tags:

views:

536

answers:

2

So I need to procedurally generate a background image for a grid, it only takes .1sec.

So I can wire into the SizeChanged event, but then when you resize the chart, it goes and fires the even maybe 30 times a second, so the resize event lags out signifigantly.

Does anybody know a good way to wire into the resize event and test weather the use is done resizing, I tried simply checking for the mouse up/down state, but when the resize event fires the mouse is pretty much always down.

+4  A: 

On resize, you could start a short lived timer (say 100 mSec), on each resize reset that timer to prevent it from elapsing. When the last resize happens, the timer will elapse, and you can draw your background image then.

Example:

Timer resizeTimer = new Timer(100) { Enabled = false };

public Window1()
{
    InitializeComponent();
    resizeTimer.Elapsed += new ElapsedEventHandler(ResizingDone);
}

void ResizingDone(object sender, ElapsedEventArgs e)
{
    resizeTimer.Stop();
    GenerateImage();
}

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
    resizeTimer.Stop();
    resizeTimer.Start();
}
Aviad P.
this seems a bit like a hack, but hey a hack that works is much better that pristine code that breaks.
Joel Barsotti
@Joel, I guess this is the only easiest way, because window resizing is actually done by Window Chrome which is not part of WPF in native window, so you will not get any such mouse up events to work with. Alternative is to create your own Window Chrome, your own style of window without borders and in your style you will need to recreate entire window frame and write the code of mouse events.
Akash Kava
I'm all for easiest, hack or not if 10 lines of code that are almost the same and 1000 lines, I'll take the 10 lines every time.
Joel Barsotti
A: 

Do you need to see the image resizing? You could handle the resize and re-render the image on mouse up event.

Gustavo Cavalcanti
That was my first thought, but when I tried it I couldn't figure out how to catch the correct mouseup event, I'm sort of a WPF newb and I'm monkey about in someone elses code. The code I was working on is inserted as a child element inside another object. What I understand is that there are bubbling and tunneling routed events, what I need to do is catch the tunneling version. My understanding is that those events are typically have a Preview prefix, and I didn't see one for mouseleftbuttonup.
Joel Barsotti
well I found the .PreviewMouseLeftButtonUpEvent event, but it doesn't seem to fire when I'm done resizing.
Joel Barsotti