やりたいこと
- HeightとWidthとThicknessを同時(?)にBindingしたい
欲しい値
Height : 140 Width : 118 Thickness : 5, 10, 5, 10
何が上手くいかないか&試したこと
- XAMLをどう編集しようが,Window_Loadedイベントが呼ばれた時に最初に書かれた方しか読み込まれなくて,Loadedイベント時に片方の値が望んだものじゃない値にされている
(Getが頭についてる行はBindingされているPropertyの読み込みのタイミングに出力してるよってことです)
上記のXamlの場合(Heightが先)の読み込み順番
- XamlのBorderThicknessの後にWidth/Heightを置いた場合でも先にした方だけが読み込まれる
- Width/HeightはViewModelの方で初期化
- ModeのTwoWayを消すとLoaded後のGetが出ないからビヨーンってどっちかに伸びたままになる
- MinWidth/MinHeight, MaxWidth/MaxHeightを設定してもLoaded時の値がおかしい
該当コード
xml
1<Window x:Class="DesktopMascots.MascotView" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:DesktopMascots" 7 mc:Ignorable="d" 8 Title="{Binding Name}" 9 Height="{Binding Height, Mode=TwoWay}" 10 Width="{Binding Width, Mode=TwoWay}" 11 Background="{Binding Chara}" 12 ShowInTaskbar="False" 13 Loaded="Window_Loaded" 14 WindowStyle="None" 15 AllowsTransparency="True" 16 ResizeMode="NoResize" 17 BorderThickness="{Binding Thickness}" > 18 <Window.DataContext> 19 <local:MascotViewModel/> 20 </Window.DataContext> 21</Window>
csharp
1private void Window_Loaded(object sender, RoutedEventArgs e) { 2 Console.WriteLine("-----Window_Loaded-----"); 3 Console.WriteLine("Height: " + Height); 4 Console.WriteLine("Width: " + Width); 5 Console.WriteLine("Thickness: " + this.BorderThickness); 6 Console.WriteLine("PrimaryHeight: " + SystemParameters.PrimaryScreenHeight); 7 Console.WriteLine("-----------------------"); 8 Left = 0; 9}
csharp
1public double Width { 2 get { 3 Console.WriteLine("Get Width: " + _width); 4 return _width; 5 } 6 set { 7 Console.WriteLine("Set Width before: " + _width); 8 Console.WriteLine("Set Width after: " + value); 9 _width = value; 10 } 11} 12public double Height { 13 get { 14 Console.WriteLine("Get Height: " + _height); 15 return _height; 16 } 17 set { 18 Console.WriteLine("Set Height before: " + _height); 19 Console.WriteLine("Set Height after: " + value); 20 _height = value; 21 } 22} 23 24private double _width, _height; 25 26public Thickness Thickness { get; set; } = new Thickness(5, 10, 5, 10); 27 28public string Name { get; set; } 29 30#endregion 31 32private MascotModel mascotModel = new MascotModel(); 33 34/// <summary> 35/// コンストラクタ 36/// HeightとWidthにThicknessを足さないと元画像より若干小さくなる 37/// </summary> 38public MascotViewModel() { 39 Chara = mascotModel.Cover.Image; 40 double thickWidth = Thickness.Right + Thickness.Left, 41 thickHeight = Thickness.Top + Thickness.Bottom; 42 _width = mascotModel.Size.Width + thickWidth; 43 _height = mascotModel.Size.Height + thickHeight; 44 Name = mascotModel.Name; 45}
ここなんでこうしてるのってのは色々説明し難い色々があるので返答出来ないですごめんなさい
Xamlの記述がおかしいのか,はたまたCSのソースコードがおかしいのか,それともこういう仕様になってしまっているのか,ご教授いただければ幸いです。
情報が足りないところがありましたら,追加します。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。