回答編集履歴
11
誤記とドキュメント構成を少し変えた
answer
CHANGED
@@ -5,10 +5,6 @@
|
|
5
5
|
|
6
6
|
Grid にバインディングするのではなく、下記のように SizeChanged のイベントを拾ってコードビハインドで依存関係プロパティに値をセットしてあげればよいと思います。
|
7
7
|
|
8
|
-
片方の ColumnDefinition Width にバインディングする方法を選んでいます。
|
9
|
-
|
10
|
-
Panel1Width と Panel2Width は両方とも実際のピクセル値がほしい認識なので、double に統一しています。なので Panel1Width をバインディングする際は GridLength に変換するコンバータを挟んでいます。もちろん要件次第では GridLength をそのまま Binding しても問題ないかと思います。
|
11
|
-
|
12
8
|
Layout2Panel.xaml
|
13
9
|
```xaml
|
14
10
|
<UserControl x:Class="WpfApp4.Layout2Panel"
|
@@ -257,9 +253,17 @@
|
|
257
253
|
|
258
254
|
```
|
259
255
|
|
256
|
+
|
257
|
+
片方の ColumnDefinition Width のみバインディングしています。
|
258
|
+
|
259
|
+
Panel1Width と Panel2Width は両方とも実際のピクセル値がほしい認識なので、double に統一しています。なので Panel1Width をバインディングする際は GridLength に変換するコンバータを挟んでいます。もちろん要件次第では GridLength をそのまま Binding しても問題ないかと思います。
|
260
|
+
|
261
|
+
|
260
262
|
Panel2 の ColumnDefinition Width はサイズを固定したくないのでスターにする必要がありますが、実際のピクセル値がほしい場合は Panel2 の Grid から ActualWidth を取得してこないと難しいのではないかと思います。
|
261
263
|
|
264
|
+
かといって Panel2 の ColumnDefinition Width に対して
|
262
|
-
|
265
|
+
`ActualWidth="{Binding Panel2Width, Mode=OneWayToSource}"`
|
266
|
+
のようなことは出来ませんから、SizeChanged イベント経由で変更しています。
|
263
267
|
|
264
268
|
また、GridSplitter は対策なしだと右側にはみ出せるため、Panel1 の ColumnDefinition には MaxWidth の設定が必要です。
|
265
269
|
Panel1 の ColumnDefinition Width から GridSplitter の Width を引くために適当な Converter を作っています。
|
10
書式を少しだけ変えた
answer
CHANGED
@@ -259,7 +259,7 @@
|
|
259
259
|
|
260
260
|
Panel2 の ColumnDefinition Width はサイズを固定したくないのでスターにする必要がありますが、実際のピクセル値がほしい場合は Panel2 の Grid から ActualWidth を取得してこないと難しいのではないかと思います。
|
261
261
|
|
262
|
-
かといって Panel2 の ColumnDefinition Width に対して
|
262
|
+
かといって Panel2 の ColumnDefinition Width に対して `AcualWidth="{Binding Panel2Width, Mode=OneWayToSource}"` のようなことは出来ませんから、SizeChanged イベント経由で変更しています。
|
263
263
|
|
264
264
|
また、GridSplitter は対策なしだと右側にはみ出せるため、Panel1 の ColumnDefinition には MaxWidth の設定が必要です。
|
265
265
|
Panel1 の ColumnDefinition Width から GridSplitter の Width を引くために適当な Converter を作っています。
|
9
文章の修正
answer
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
~~Grid にバインディングするのではなく、下記のように SizeChanged のイベントを拾ってコードビハインドで依存関係プロパティに値をセットしてあげればよいと思います。~~
|
2
|
-
|
3
1
|
TN8001 さんから頂いたコメントで復元について考慮されていないことに気づいたので改めて手を加えました。
|
4
|
-
(以前のソースコードについては本文から削除しています。必要があれば変更履歴で追って頂ければと思います)
|
2
|
+
(以前のソースコードについては本文から削除しています。そこまで大きな変更がある訳ではありませんが、必要があれば変更履歴で追って頂ければと思います)
|
5
3
|
|
6
|
-
tuyudaku さんのコメントと同じように ColumnDefinition Width にバインディングしています。
|
7
|
-
Panel1Width と Panel2Width は両方とも実際のピクセル値がほしい認識なので、double で持ちバインディングする際は GridLength に変換するコンバータを挟んでいます。
|
8
|
-
|
4
|
+
---
|
9
5
|
|
6
|
+
Grid にバインディングするのではなく、下記のように SizeChanged のイベントを拾ってコードビハインドで依存関係プロパティに値をセットしてあげればよいと思います。
|
7
|
+
|
8
|
+
片方の ColumnDefinition Width にバインディングする方法を選んでいます。
|
9
|
+
|
10
|
+
Panel1Width と Panel2Width は両方とも実際のピクセル値がほしい認識なので、double に統一しています。なので Panel1Width をバインディングする際は GridLength に変換するコンバータを挟んでいます。もちろん要件次第では GridLength をそのまま Binding しても問題ないかと思います。
|
11
|
+
|
10
12
|
Layout2Panel.xaml
|
11
13
|
```xaml
|
12
14
|
<UserControl x:Class="WpfApp4.Layout2Panel"
|
@@ -257,10 +259,12 @@
|
|
257
259
|
|
258
260
|
Panel2 の ColumnDefinition Width はサイズを固定したくないのでスターにする必要がありますが、実際のピクセル値がほしい場合は Panel2 の Grid から ActualWidth を取得してこないと難しいのではないかと思います。
|
259
261
|
|
262
|
+
かといって Panel2 の ColumnDefinition Width に対して ActualWidth="{Binding Panel2Width, Mode=OneWayToSource}" のようなことは出来ませんから、SizeChanged イベント経由で変更しています。
|
263
|
+
|
260
|
-
また
|
264
|
+
また、GridSplitter は対策なしだと右側にはみ出せるため、Panel1 の ColumnDefinition には MaxWidth の設定が必要です。
|
261
265
|
Panel1 の ColumnDefinition Width から GridSplitter の Width を引くために適当な Converter を作っています。
|
262
266
|
[WPFのGridSplitterでピクセル指定の時に画面外までサイズ変更できなくする](https://blog.okazuki.jp/entry/2016/01/08/205031)
|
263
267
|
|
264
268
|

|
265
269
|
|
266
|
-
ファイルIOに関しては本題ではない認識なので実装を割愛しています。実際は設計にもよると思いますが、MVVM で製作しているならば Model で保存、読み込みの処理を行い、掲示した自分のコードの
|
270
|
+
ファイルIO周りに関しては本題ではない認識なので実装を割愛しています。実際は設計にもよると思いますが、MVVM で製作しているならば Model で保存、読み込みの処理を行い、掲示した自分のコードのようなケースであれば Model.Panel1Width に初期値を渡してあげれば良いかと思います。
|
8
文章の修正
answer
CHANGED
@@ -5,6 +5,7 @@
|
|
5
5
|
|
6
6
|
tuyudaku さんのコメントと同じように ColumnDefinition Width にバインディングしています。
|
7
7
|
Panel1Width と Panel2Width は両方とも実際のピクセル値がほしい認識なので、double で持ちバインディングする際は GridLength に変換するコンバータを挟んでいます。
|
8
|
+
もちろん GridLength をそのまま Binding しても問題ないかと思います。
|
8
9
|
|
9
10
|
Layout2Panel.xaml
|
10
11
|
```xaml
|
7
文章の修正
answer
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
(以前のソースコードについては本文から削除しています。必要があれば変更履歴で追って頂ければと思います)
|
5
5
|
|
6
6
|
tuyudaku さんのコメントと同じように ColumnDefinition Width にバインディングしています。
|
7
|
+
Panel1Width と Panel2Width は両方とも実際のピクセル値がほしい認識なので、double で持ちバインディングする際は GridLength に変換するコンバータを挟んでいます。
|
7
8
|
|
8
9
|
Layout2Panel.xaml
|
9
10
|
```xaml
|
6
画像追加
answer
CHANGED
@@ -259,4 +259,6 @@
|
|
259
259
|
Panel1 の ColumnDefinition Width から GridSplitter の Width を引くために適当な Converter を作っています。
|
260
260
|
[WPFのGridSplitterでピクセル指定の時に画面外までサイズ変更できなくする](https://blog.okazuki.jp/entry/2016/01/08/205031)
|
261
261
|
|
262
|
+

|
263
|
+
|
262
264
|
ファイルIOに関しては本題ではない認識なので実装を割愛しています。実際は設計にもよると思いますが、MVVM で製作しているならば Model で保存、読み込みの処理を行い、掲示した自分のコードの場合は Model.Panel1Width に初期値として渡してあげれば良いかと思います。
|
5
コメントの反映&コード修正&追記
answer
CHANGED
@@ -1,5 +1,10 @@
|
|
1
|
-
Grid に
|
1
|
+
~~Grid にバインディングするのではなく、下記のように SizeChanged のイベントを拾ってコードビハインドで依存関係プロパティに値をセットしてあげればよいと思います。~~
|
2
2
|
|
3
|
+
TN8001 さんから頂いたコメントで復元について考慮されていないことに気づいたので改めて手を加えました。
|
4
|
+
(以前のソースコードについては本文から削除しています。必要があれば変更履歴で追って頂ければと思います)
|
5
|
+
|
6
|
+
tuyudaku さんのコメントと同じように ColumnDefinition Width にバインディングしています。
|
7
|
+
|
3
8
|
Layout2Panel.xaml
|
4
9
|
```xaml
|
5
10
|
<UserControl x:Class="WpfApp4.Layout2Panel"
|
@@ -10,9 +15,14 @@
|
|
10
15
|
xmlns:local="clr-namespace:WpfApp4"
|
11
16
|
mc:Ignorable="d"
|
12
17
|
d:DesignHeight="450" d:DesignWidth="800">
|
13
|
-
<
|
18
|
+
<UserControl.Resources>
|
19
|
+
<local:GridLengthConverter x:Key="gridLengthConverter" />
|
20
|
+
<local:MinusConverter x:Key="minusConverter" />
|
21
|
+
</UserControl.Resources>
|
22
|
+
<Grid x:Name="LayoutRoot">
|
14
23
|
<Grid.ColumnDefinitions>
|
15
|
-
<ColumnDefinition Width="
|
24
|
+
<ColumnDefinition Width="{Binding Path=Panel1Width, Mode=TwoWay, Converter={StaticResource gridLengthConverter}}"
|
25
|
+
MaxWidth="{Binding ActualWidth, ElementName=LayoutRoot, Mode=OneWay, Converter={StaticResource minusConverter}}"/>
|
16
26
|
<ColumnDefinition Width="1"/>
|
17
27
|
<ColumnDefinition Width="*"/>
|
18
28
|
</Grid.ColumnDefinitions>
|
@@ -25,11 +35,10 @@
|
|
25
35
|
</Grid>
|
26
36
|
</Grid>
|
27
37
|
</UserControl>
|
28
|
-
|
29
38
|
```
|
30
39
|
|
31
40
|
Layout2Panel.xaml.cs
|
32
|
-
```
|
41
|
+
```xaml
|
33
42
|
using System;
|
34
43
|
using System.Collections.Generic;
|
35
44
|
using System.ComponentModel;
|
@@ -91,7 +100,6 @@
|
|
91
100
|
}
|
92
101
|
}
|
93
102
|
```
|
94
|
-
|
95
103
|
MainWindow.xaml
|
96
104
|
```xaml
|
97
105
|
<Window x:Class="WpfApp4.MainWindow"
|
@@ -101,7 +109,7 @@
|
|
101
109
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
102
110
|
xmlns:local="clr-namespace:WpfApp4"
|
103
111
|
mc:Ignorable="d"
|
104
|
-
d:DataContext="{d:DesignInstance local:
|
112
|
+
d:DataContext="{d:DesignInstance local:Model}"
|
105
113
|
Title="MainWindow" Height="450" Width="800">
|
106
114
|
<Grid>
|
107
115
|
<Grid.RowDefinitions>
|
@@ -118,7 +126,7 @@
|
|
118
126
|
```
|
119
127
|
|
120
128
|
MainWindow.xaml.cs
|
121
|
-
```
|
129
|
+
```
|
122
130
|
using System;
|
123
131
|
using System.Collections.Generic;
|
124
132
|
using System.ComponentModel;
|
@@ -146,7 +154,7 @@
|
|
146
154
|
public MainWindow()
|
147
155
|
{
|
148
156
|
InitializeComponent();
|
149
|
-
DataContext = new
|
157
|
+
DataContext = new Model() { Panel1Width = 300, Panel2Width = 500 };
|
150
158
|
}
|
151
159
|
}
|
152
160
|
|
@@ -165,7 +173,7 @@
|
|
165
173
|
}
|
166
174
|
}
|
167
175
|
|
168
|
-
public class
|
176
|
+
public class Model : BindableBase
|
169
177
|
{
|
170
178
|
private double _panel1Width;
|
171
179
|
|
@@ -183,7 +191,72 @@
|
|
183
191
|
}
|
184
192
|
}
|
185
193
|
}
|
194
|
+
```
|
186
195
|
|
196
|
+
GridLengthConveter.cs
|
197
|
+
```cs
|
198
|
+
using System;
|
199
|
+
using System.Collections.Generic;
|
200
|
+
using System.Globalization;
|
201
|
+
using System.Linq;
|
202
|
+
using System.Text;
|
203
|
+
using System.Threading.Tasks;
|
204
|
+
using System.Windows;
|
205
|
+
using System.Windows.Data;
|
206
|
+
|
207
|
+
namespace WpfApp4
|
208
|
+
{
|
209
|
+
public class GridLengthConverter : IValueConverter
|
210
|
+
{
|
211
|
+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
212
|
+
{
|
213
|
+
double val = (double)value;
|
214
|
+
GridLength gridLength = new GridLength(val);
|
215
|
+
|
216
|
+
return gridLength;
|
217
|
+
}
|
218
|
+
|
219
|
+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
220
|
+
{
|
221
|
+
GridLength val = (GridLength)value;
|
222
|
+
|
223
|
+
return val.Value;
|
224
|
+
}
|
225
|
+
}
|
226
|
+
}
|
187
227
|
```
|
228
|
+
MinusConverter.cs
|
229
|
+
```cs
|
230
|
+
using System;
|
231
|
+
using System.Collections.Generic;
|
232
|
+
using System.Globalization;
|
233
|
+
using System.Linq;
|
234
|
+
using System.Text;
|
235
|
+
using System.Threading.Tasks;
|
236
|
+
using System.Windows.Data;
|
188
237
|
|
238
|
+
namespace WpfApp4
|
239
|
+
{
|
240
|
+
public class MinusConverter : IValueConverter
|
241
|
+
{
|
242
|
+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
243
|
+
{
|
244
|
+
return ((double)value) - 1.0;
|
245
|
+
}
|
246
|
+
|
247
|
+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
248
|
+
{
|
249
|
+
throw new NotImplementedException();
|
250
|
+
}
|
251
|
+
}
|
252
|
+
}
|
253
|
+
|
254
|
+
```
|
255
|
+
|
256
|
+
Panel2 の ColumnDefinition Width はサイズを固定したくないのでスターにする必要がありますが、実際のピクセル値がほしい場合は Panel2 の Grid から ActualWidth を取得してこないと難しいのではないかと思います。
|
257
|
+
|
258
|
+
また GridSplitter は右側にはみ出すことができるため、Panel1 の ColumnDefinition には MaxWidth の設定が必要です。
|
189
|
-
|
259
|
+
Panel1 の ColumnDefinition Width から GridSplitter の Width を引くために適当な Converter を作っています。
|
260
|
+
[WPFのGridSplitterでピクセル指定の時に画面外までサイズ変更できなくする](https://blog.okazuki.jp/entry/2016/01/08/205031)
|
261
|
+
|
262
|
+
ファイルIOに関しては本題ではない認識なので実装を割愛しています。実際は設計にもよると思いますが、MVVM で製作しているならば Model で保存、読み込みの処理を行い、掲示した自分のコードの場合は Model.Panel1Width に初期値として渡してあげれば良いかと思います。
|
4
文章の修正
answer
CHANGED
@@ -186,4 +186,4 @@
|
|
186
186
|
|
187
187
|
```
|
188
188
|
|
189
|
-
Layout2Panel の Panel1Width, Panel2Width がイベントにより書き換わることで、
|
189
|
+
Layout2Panel の Panel1Width, Panel2Width がイベントにより書き換わることで、データソースが反映されていく様子を MainWindow の UI 上から確認することができます。
|
3
ポイントとなるソースが上にくるようにした
answer
CHANGED
@@ -1,5 +1,97 @@
|
|
1
1
|
Grid に Binding するのではなく、下記のように SizeChanged のイベントを拾ってコードビハインドで依存関係プロパティに値をセットしてあげればよいと思います。
|
2
2
|
|
3
|
+
Layout2Panel.xaml
|
4
|
+
```xaml
|
5
|
+
<UserControl x:Class="WpfApp4.Layout2Panel"
|
6
|
+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
7
|
+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
8
|
+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
9
|
+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
10
|
+
xmlns:local="clr-namespace:WpfApp4"
|
11
|
+
mc:Ignorable="d"
|
12
|
+
d:DesignHeight="450" d:DesignWidth="800">
|
13
|
+
<Grid>
|
14
|
+
<Grid.ColumnDefinitions>
|
15
|
+
<ColumnDefinition Width="*"/>
|
16
|
+
<ColumnDefinition Width="1"/>
|
17
|
+
<ColumnDefinition Width="*"/>
|
18
|
+
</Grid.ColumnDefinitions>
|
19
|
+
<Grid x:Name="Panel1" Grid.Column="0" Background="Red" HorizontalAlignment="Stretch" SizeChanged="PanelSizeChanged" >
|
20
|
+
<TextBlock Text="①" TextAlignment="Center" VerticalAlignment="Center" FontSize="30" FontWeight="UltraBold"/>
|
21
|
+
</Grid>
|
22
|
+
<GridSplitter Grid.Column="1" ResizeDirection="Columns" HorizontalAlignment="Stretch"/>
|
23
|
+
<Grid x:Name="Panel2" Grid.Column="2" Background="Blue" HorizontalAlignment="Stretch" SizeChanged="PanelSizeChanged">
|
24
|
+
<TextBlock Text="②" TextAlignment="Center" VerticalAlignment="Center" FontSize="30" FontWeight="UltraBold"/>
|
25
|
+
</Grid>
|
26
|
+
</Grid>
|
27
|
+
</UserControl>
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
Layout2Panel.xaml.cs
|
32
|
+
```cs
|
33
|
+
using System;
|
34
|
+
using System.Collections.Generic;
|
35
|
+
using System.ComponentModel;
|
36
|
+
using System.Linq;
|
37
|
+
using System.Runtime.CompilerServices;
|
38
|
+
using System.Text;
|
39
|
+
using System.Threading.Tasks;
|
40
|
+
using System.Windows;
|
41
|
+
using System.Windows.Controls;
|
42
|
+
using System.Windows.Data;
|
43
|
+
using System.Windows.Documents;
|
44
|
+
using System.Windows.Input;
|
45
|
+
using System.Windows.Media;
|
46
|
+
using System.Windows.Media.Imaging;
|
47
|
+
using System.Windows.Navigation;
|
48
|
+
using System.Windows.Shapes;
|
49
|
+
|
50
|
+
namespace WpfApp4
|
51
|
+
{
|
52
|
+
/// <summary>
|
53
|
+
/// Panel.xaml の相互作用ロジック
|
54
|
+
/// </summary>
|
55
|
+
public partial class Layout2Panel : UserControl
|
56
|
+
{
|
57
|
+
public Layout2Panel()
|
58
|
+
{
|
59
|
+
InitializeComponent();
|
60
|
+
}
|
61
|
+
|
62
|
+
public double Panel1Width
|
63
|
+
{
|
64
|
+
get => (double)GetValue(Panel1WidthProperty);
|
65
|
+
set => SetValue(Panel1WidthProperty, value);
|
66
|
+
}
|
67
|
+
public static readonly DependencyProperty Panel1WidthProperty =
|
68
|
+
DependencyProperty.Register(
|
69
|
+
"Panel1Width",
|
70
|
+
typeof(double),
|
71
|
+
typeof(Layout2Panel),
|
72
|
+
new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
|
73
|
+
|
74
|
+
public double Panel2Width
|
75
|
+
{
|
76
|
+
get => (double)GetValue(Panel2WidthProperty);
|
77
|
+
set => SetValue(Panel2WidthProperty, value);
|
78
|
+
}
|
79
|
+
public static readonly DependencyProperty Panel2WidthProperty =
|
80
|
+
DependencyProperty.Register(
|
81
|
+
"Panel2Width",
|
82
|
+
typeof(double),
|
83
|
+
typeof(Layout2Panel),
|
84
|
+
new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
|
85
|
+
|
86
|
+
private void PanelSizeChanged(object sender, SizeChangedEventArgs e)
|
87
|
+
{
|
88
|
+
Panel1Width = Panel1.ActualWidth;
|
89
|
+
Panel2Width = Panel2.ActualWidth;
|
90
|
+
}
|
91
|
+
}
|
92
|
+
}
|
93
|
+
```
|
94
|
+
|
3
95
|
MainWindow.xaml
|
4
96
|
```xaml
|
5
97
|
<Window x:Class="WpfApp4.MainWindow"
|
@@ -94,96 +186,4 @@
|
|
94
186
|
|
95
187
|
```
|
96
188
|
|
97
|
-
Layout2Panel.xaml
|
98
|
-
```xaml
|
99
|
-
<UserControl x:Class="WpfApp4.Layout2Panel"
|
100
|
-
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
101
|
-
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
102
|
-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
103
|
-
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
104
|
-
xmlns:local="clr-namespace:WpfApp4"
|
105
|
-
mc:Ignorable="d"
|
106
|
-
d:DesignHeight="450" d:DesignWidth="800">
|
107
|
-
<Grid>
|
108
|
-
<Grid.ColumnDefinitions>
|
109
|
-
<ColumnDefinition Width="*"/>
|
110
|
-
<ColumnDefinition Width="1"/>
|
111
|
-
<ColumnDefinition Width="*"/>
|
112
|
-
</Grid.ColumnDefinitions>
|
113
|
-
<Grid x:Name="Panel1" Grid.Column="0" Background="Red" HorizontalAlignment="Stretch" SizeChanged="PanelSizeChanged" >
|
114
|
-
<TextBlock Text="①" TextAlignment="Center" VerticalAlignment="Center" FontSize="30" FontWeight="UltraBold"/>
|
115
|
-
</Grid>
|
116
|
-
<GridSplitter Grid.Column="1" ResizeDirection="Columns" HorizontalAlignment="Stretch"/>
|
117
|
-
<Grid x:Name="Panel2" Grid.Column="2" Background="Blue" HorizontalAlignment="Stretch" SizeChanged="PanelSizeChanged">
|
118
|
-
<TextBlock Text="②" TextAlignment="Center" VerticalAlignment="Center" FontSize="30" FontWeight="UltraBold"/>
|
119
|
-
</Grid>
|
120
|
-
</Grid>
|
121
|
-
</UserControl>
|
122
|
-
|
123
|
-
```
|
124
|
-
|
125
|
-
Layout2Panel.xaml.cs
|
126
|
-
```cs
|
127
|
-
using System;
|
128
|
-
using System.Collections.Generic;
|
129
|
-
using System.ComponentModel;
|
130
|
-
using System.Linq;
|
131
|
-
using System.Runtime.CompilerServices;
|
132
|
-
using System.Text;
|
133
|
-
using System.Threading.Tasks;
|
134
|
-
using System.Windows;
|
135
|
-
using System.Windows.Controls;
|
136
|
-
using System.Windows.Data;
|
137
|
-
using System.Windows.Documents;
|
138
|
-
using System.Windows.Input;
|
139
|
-
using System.Windows.Media;
|
140
|
-
using System.Windows.Media.Imaging;
|
141
|
-
using System.Windows.Navigation;
|
142
|
-
using System.Windows.Shapes;
|
143
|
-
|
144
|
-
namespace WpfApp4
|
145
|
-
{
|
146
|
-
/// <summary>
|
147
|
-
/// Panel.xaml の相互作用ロジック
|
148
|
-
/// </summary>
|
149
|
-
public partial class Layout2Panel : UserControl
|
150
|
-
{
|
151
|
-
public Layout2Panel()
|
152
|
-
{
|
153
|
-
InitializeComponent();
|
154
|
-
}
|
155
|
-
|
156
|
-
public double Panel1Width
|
157
|
-
{
|
158
|
-
get => (double)GetValue(Panel1WidthProperty);
|
159
|
-
set => SetValue(Panel1WidthProperty, value);
|
160
|
-
}
|
161
|
-
public static readonly DependencyProperty Panel1WidthProperty =
|
162
|
-
DependencyProperty.Register(
|
163
|
-
"Panel1Width",
|
164
|
-
typeof(double),
|
165
|
-
typeof(Layout2Panel),
|
166
|
-
new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
|
167
|
-
|
168
|
-
public double Panel2Width
|
169
|
-
{
|
170
|
-
get => (double)GetValue(Panel2WidthProperty);
|
171
|
-
set => SetValue(Panel2WidthProperty, value);
|
172
|
-
}
|
173
|
-
public static readonly DependencyProperty Panel2WidthProperty =
|
174
|
-
DependencyProperty.Register(
|
175
|
-
"Panel2Width",
|
176
|
-
typeof(double),
|
177
|
-
typeof(Layout2Panel),
|
178
|
-
new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
|
179
|
-
|
180
|
-
private void PanelSizeChanged(object sender, SizeChangedEventArgs e)
|
181
|
-
{
|
182
|
-
Panel1Width = Panel1.ActualWidth;
|
183
|
-
Panel2Width = Panel2.ActualWidth;
|
184
|
-
}
|
185
|
-
}
|
186
|
-
}
|
187
|
-
```
|
188
|
-
|
189
|
-
Layout2Panel の Panel1Width, Panel2Width が書き換わ
|
189
|
+
Layout2Panel の Panel1Width, Panel2Width がイベントにより書き換わることで、MainWindow から データソースへ反映されていく様子を確認することができます。
|
2
文章の修正
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Grid に Binding するのではなく、下記のように SizeChanged のイベントを拾ってコードビハインドで依存プロパティに値をセットしてあげればよいと思います。
|
1
|
+
Grid に Binding するのではなく、下記のように SizeChanged のイベントを拾ってコードビハインドで依存関係プロパティに値をセットしてあげればよいと思います。
|
2
2
|
|
3
3
|
MainWindow.xaml
|
4
4
|
```xaml
|
1
コード修正
answer
CHANGED
@@ -9,7 +9,7 @@
|
|
9
9
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
10
10
|
xmlns:local="clr-namespace:WpfApp4"
|
11
11
|
mc:Ignorable="d"
|
12
|
-
d:DataContext="{d:DesignInstance local:
|
12
|
+
d:DataContext="{d:DesignInstance local:ViewModel}"
|
13
13
|
Title="MainWindow" Height="450" Width="800">
|
14
14
|
<Grid>
|
15
15
|
<Grid.RowDefinitions>
|
@@ -17,13 +17,12 @@
|
|
17
17
|
<RowDefinition Height="*"/>
|
18
18
|
</Grid.RowDefinitions>
|
19
19
|
<StackPanel Grid.Row="0" Orientation="Horizontal">
|
20
|
-
<TextBlock Text="{Binding Panel1Width
|
20
|
+
<TextBlock Text="{Binding Panel1Width}"/>
|
21
|
-
<TextBlock Text="{Binding Panel2Width
|
21
|
+
<TextBlock Text="{Binding Panel2Width}"/>
|
22
22
|
</StackPanel>
|
23
23
|
<local:Layout2Panel Grid.Row="1" Panel1Width="{Binding Panel1Width}" Panel2Width="{Binding Panel2Width}"/>
|
24
24
|
</Grid>
|
25
25
|
</Window>
|
26
|
-
|
27
26
|
```
|
28
27
|
|
29
28
|
MainWindow.xaml.cs
|
@@ -74,7 +73,6 @@
|
|
74
73
|
}
|
75
74
|
}
|
76
75
|
|
77
|
-
|
78
76
|
public class ViewModel : BindableBase
|
79
77
|
{
|
80
78
|
private double _panel1Width;
|