I need catch the event endresize in WPF.
Answer: Use the addEventListener() Method You can simply use the addEventListener() method to register an event handler to listen for the browser window resize event, such as window. addEventListener('resize', ...) . The following example will display the current width and height of the browser window on resize.
Press Alt + Spacebar again to open the window menu, arrow down to Size, and press Enter . Press the up or down arrow key if you want to resize the window vertically or the left or right arrow key if you want to resize horizontally.
The resize event fires when the document view (window) has been resized. This event is not cancelable and does not bubble. In some earlier browsers it was possible to register resize event handlers on any HTML element.
WPF doesn't provide an event that solely fires at the end of the resize process. SizeChanged is the only event associated with Window resizing - and it will fire multiple times during the resizing process.
A total hack would be to constantly set a timer ticking when the SizeChanged event fires. Then timer will not get a chance to tick until resizing ends and at that point do your one time processing.
public MyUserControl() { _resizeTimer.Tick += _resizeTimer_Tick; } DispatcherTimer _resizeTimer = new DispatcherTimer { Interval = new TimeSpan(0, 0, 0, 0, 1500), IsEnabled = false }; private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e) { _resizeTimer.IsEnabled = true; _resizeTimer.Stop(); _resizeTimer.Start(); } void _resizeTimer_Tick(object sender, EventArgs e) { _resizeTimer.IsEnabled = false; //Do end of resize processing }
Reactive Extensions for .NET provides some really cool capabilities for dealing with standard event patterns including being able to throttle events. I had a similar problem in dealing with size changed events and while the solution is still somewhat "hacky" I think that Reactive Extensions provides a much more elegant way of implementing it. Here is my implementation:
IObservable<SizeChangedEventArgs> ObservableSizeChanges = Observable .FromEventPattern<SizeChangedEventArgs>(this, "SizeChanged") .Select(x => x.EventArgs) .Throttle(TimeSpan.FromMilliseconds(200)); IDisposable SizeChangedSubscription = ObservableSizeChanges .ObserveOn(SynchronizationContext.Current) .Subscribe(x => { Size_Changed(x); });
This will effectively throttle the SizeChanged
event such that your Size_Changed method (where you can execute custom code) will not be executed until 200 milliseconds (or however long you would like to wait) have passed without another SizeChanged
event being fired.
private void Size_Changed(SizeChangedEventArgs e) { // custom code for dealing with end of size changed here }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With