Hello. How can I change the size of the window using the mouse button not dynamically (i.e. immediately while dragging), but only after releasing it?

You need something like the ShowsPreview property of the ShowsPreview element. Similar for Window did not find.

  • You want a strange, applications on the system should look and behave monotonous. But you can, if you try. Make a custom non-client area, handle the window resizing yourself. - VladD
  • @VladD, you need not to see the brakes rendering the contents of the window. Campaign need to change the content settings. - Gardes
  • Well, you can, brute force can not resist anything. But it is better to still fix the rest of the code so that it is redrawn quickly. - VladD

1 answer 1

You can, for example, subscribe to the window resizing, and set the size of the content manually only when the window “calms down”.

The shortest solution is obtained with Rx extensions (install System.Reactive via nuget ):

 <Window x:Class="CustomNC.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Test" Height="350" Width="525"> <Grid Name="ClientArea"> <Grid Name="ContentHolder" HorizontalAlignment="Left" VerticalAlignment="Top"> <!-- тут ваш контент --> </Grid> </Grid> </Window> 

and code-behind:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); Observable.FromEventPattern<SizeChangedEventArgs>(ClientArea, nameof(SizeChanged)) .Throttle(TimeSpan.FromSeconds(0.3)) .ObserveOnDispatcher() .Subscribe(args => { ContentHolder.Width = ClientArea.ActualWidth; ContentHolder.Height = ClientArea.ActualHeight; }); } } 

Result:

animated cartoon


The same is easy to get with a more prosaic code, without Rx. In XAML, SizeChanged to SizeChanged :

 <Grid Name="ClientArea" SizeChanged="OnSizeChanged"> <Grid Name="ContentHolder" HorizontalAlignment="Left" VerticalAlignment="Top"> <!-- тут ваш контент --> </Grid> </Grid> 

and in code-behind:

 Task currentWaitTask = null; async void OnSizeChanged(object sender, SizeChangedEventArgs e) { var waitTask = Task.Delay(TimeSpan.FromSeconds(0.3)); currentWaitTask = waitTask; await waitTask; if (currentWaitTask != waitTask) return; currentWaitTask = null; ContentHolder.Width = ClientArea.ActualWidth; ContentHolder.Height = ClientArea.ActualHeight; } 

Another variation for lovers of unfading classics, with a timer and without TPL:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); timer.Tick += (o, args) => { ContentHolder.Width = ClientArea.ActualWidth; ContentHolder.Height = ClientArea.ActualHeight; timer.Stop(); }; } DispatcherTimer timer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(0.3) }; void OnSizeChanged(object sender, SizeChangedEventArgs e) { timer.Stop(); timer.Start(); } } 
  • Everything works, I used option 2. - Gardes
  • @ S.Kost: Well, that's good. - VladD
  • Yes. Thanks you! - Gardes
  • @ S.Kost: Please! Added another option. - VladD