しかし、ウィンドウの起動時にSizeChagedイベントは発生しません。
コードビハインドなら来るんですけど、確かになぜか来ないですね...
(Interaction.Triggers
の?)タイミング的な問題だとは思うんですが、いろいろ試しても解決できませんでした。
コードビハインドなら来るのはわかっているので、雑にビヘイビアにしました。
ActualWidth
・ActualHeight
なので、もちろん設定はできません(OneWayToSource
相当)
xml
1 < Window
2 x: Class = " Qfpwwvzt0zl808x.Views.MainWindow "
3 xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
4 xmlns: x = " http://schemas.microsoft.com/winfx/2006/xaml "
5 xmlns: i = " http://schemas.microsoft.com/xaml/behaviors "
6 xmlns: prism = " http://prismlibrary.com/ "
7 xmlns: views = " clr-namespace:Qfpwwvzt0zl808x.Views "
8 Title = " {Binding Title} "
9 Width = " {Binding WindowWidth, Mode=TwoWay} "
10 Height = " {Binding WindowHeight, Mode=TwoWay} "
11 prism: ViewModelLocator.AutoWireViewModel = " True " >
12 < Grid >
13 < Rectangle
14 SizeChanged = " Rectangle_SizeChanged "
15 Stroke = " Aqua "
16 StrokeThickness = " 10 " >
17 < i: Interaction.Behaviors >
18 < views: ActualSizeBehavior ActualSize = " {Binding RectSize} " />
19 </ i: Interaction.Behaviors >
20 </ Rectangle >
21 </ Grid >
22 </ Window >
cs
1 using System . Diagnostics ;
2 using System . Windows ;
3 using Microsoft . Xaml . Behaviors ;
4
5 namespace Qfpwwvzt0zl808x . Views ;
6
7
8 public partial class MainWindow : Window
9 {
10 public MainWindow ( ) => InitializeComponent ( ) ;
11
12 private void Rectangle_SizeChanged ( object sender , SizeChangedEventArgs e )
13 => Debug . WriteLine ( $"Rectangle_SizeChanged: { e . NewSize } " ) ;
14 }
15
16 public class ActualSizeBehavior : Behavior < FrameworkElement >
17 {
18 public Size ActualSize { get => ( Size ) GetValue ( ActualSizeProperty ) ; set => SetValue ( ActualSizeProperty , value ) ; }
19 public static readonly DependencyProperty ActualSizeProperty
20 = DependencyProperty . Register ( nameof ( ActualSize ) , typeof ( Size ) , typeof ( ActualSizeBehavior ) ,
21 new FrameworkPropertyMetadata ( new Size ( ) , FrameworkPropertyMetadataOptions . BindsTwoWayByDefault ) ) ;
22
23 protected override void OnAttached ( ) => AssociatedObject . SizeChanged += AssociatedObject_SizeChanged ;
24 protected override void OnDetaching ( ) => AssociatedObject . SizeChanged -= AssociatedObject_SizeChanged ;
25
26 private void AssociatedObject_SizeChanged ( object sender , SizeChangedEventArgs e )
27 {
28 ActualSize = new Size ( AssociatedObject . ActualWidth , AssociatedObject . ActualHeight ) ;
29 }
30 }
cs
1 using System . ComponentModel ;
2 using System . Diagnostics ;
3 using System . Windows ;
4 using Prism . Mvvm ;
5
6 namespace Qfpwwvzt0zl808x . ViewModels ;
7
8
9 public class MainWindowViewModel : BindableBase
10 {
11 public string Title { get => _title ; set => SetProperty ( ref _title , value ) ; }
12 private string _title = "Prism Application" ;
13
14 public double WindowWidth { get => _WindowWidth ; set => SetProperty ( ref _WindowWidth , value ) ; }
15 private double _WindowWidth ;
16
17 public double WindowHeight { get => _WindowHeight ; set => SetProperty ( ref _WindowHeight , value ) ; }
18 private double _WindowHeight ;
19
20 public Size RectSize { get => _RectSize ; set => SetProperty ( ref _RectSize , value ) ; }
21 private Size _RectSize ;
22
23
24 public MainWindowViewModel ( )
25 {
26 // ファイルから読んだりするテイ
27 WindowWidth = 525 ;
28 WindowHeight = 350 ;
29
30 PropertyChanged += MainWindowViewModel_PropertyChanged ;
31 }
32
33 private void MainWindowViewModel_PropertyChanged ( object sender , PropertyChangedEventArgs e )
34 {
35 if ( e . PropertyName == nameof ( RectSize ) ) Debug . WriteLine ( $" { nameof ( RectSize ) } : { RectSize } " ) ;
36
37 if ( e . PropertyName == nameof ( WindowWidth ) ) Debug . WriteLine ( $" { nameof ( WindowWidth ) } : { WindowWidth } " ) ;
38 if ( e . PropertyName == nameof ( WindowHeight ) ) Debug . WriteLine ( $" { nameof ( WindowHeight ) } : { WindowHeight } " ) ;
39 }
40 }
Loadedイベントでコントロールの表示時のWidthとHeightを取得して解決しました。
var view = e.Source as Views.MainWindow;
MainWindow
のサイズ保存・復元が目的なら、単純にWidth
・Height
にバインドで十分です。