Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
マイクロソフトの田中達彦です。
「WPFへの道」という連載は、Windowsフォームをお使いの方がWPFに移行する際に、壁になってしまう点の解決策を紹介していきます。
不定期に情報を追加します。
[困ったこと]
WPFのフォームにコントロールを貼り付け、AnchorプロパティやDockプロパティを設定したいのですが、それらに該当するプロパティが見つかりません。
[解決策] Width(横の幅)、Height(高さ)、HorizontalAlignment(横方向の位置)、VerticalAlignment(縦方向の位置)、Margin(余白)の各プロパティを使用します。
Width、Heightプロパティに固定値を入れた場合は、外側のウィンドウの大きさを変更しても、コントロールの大きさ(幅または高さ)は変わりません。
Width、HeightプロパティにAutoを入れた場合は、外側のウィンドウの大きさに応じてコントロールの大きさが変わります。
HorizontalAlignment、VerticalAlignmentは横方向または縦方向の位置を設定するプロパティです。HorizontalAlignmentの場合は、Leftを設定するとWindowsフォームのAnchorをLeftに設定することと同じ効果が得られ、Rightを設定するとAnchorをRightに設定することと同じ効果が得られます。
Stretchを設定すると、AnchorにLeftとRightを同時に設定した効果が得られます。ただし、Stretchの場合はWidthプロパティにAutoを設定しないと効果が得られません。
Marginプロパティと併用すると、Dockプロパティと同じ効果が得られます。
4辺すべてにアンカーを設定したい場合は、Width、HeightプロパティをAutoに、HorizontalAlignmentとVerticalAlignmentをStretchに変更してください。
ドックをFillにしたい場合は、さらにMarginプロパティの値をリセットします。
WidthまたはHeightプロパティをAuto、HorizontalAlignmentまたはVerticalAlignmentをStretch、Marginプロパティをゼロにするには、下図のようにプロパティウィンドウでプロパティ名と値が表示されている場所の間に表示されている印をクリックし、[値のリセット]を選択してください。
マイクロソフト
田中達彦
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in