开发者

Method called in WPF when window size changes?

I am creating a application using WPF. I w开发者_运维百科ant to calculate the coordinate of the visible region of a canvas.

Which method will be called when i resize the window, so that i can calculate the coordinate when the window is resized?


The SizeChanged event will be raised, you can add an event handler there. But maybe your particular problem has another solution (there's too little information in your question).


Provide SizeChanged="Window_SizeChanged" just like below:

<Window x:Class="SalesPlus.DataViews"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Contacts"
    Height="600"
    Width="1200"
    SizeChanged="Window_SizeChanged">

Then in your Window_SizeChanged method you can have the updated sizes as follows:

        private void Window_SizeChanged (object sender, SizeChangedEventArgs e)
        {

        var ah = ActualHeight;
        var aw = ActualWidth;
        var h = Height;
        var w = Width;
        Console.WriteLine ("ActualHeight(updated height value): {0}, ActualWidth(updated width value): {1}, Height(before size change): {2}, Width(before size change): {3}", ah, aw, h, w);
        }
//output:
// ActualHeight(updated height): 744, ActualWidth(updated width): 1382, Height(before size change): 600, Width(before size change): 1200
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜