WPF で UserControl を作成するとき、Visual Studio デザイナで変更を表示できるように、それに任意の Height 値と Width 値を与えると便利です。ただし、コントロールを実行するときは、Height と Width を未定義にして、コントロールを配置したコンテナに合わせて拡張する必要があります。前に Height と Width の値を削除せずに、これと同じ機能を実現するにはどうすればよいですか?私のコントロールを構築していますか?(または、親コンテナーで DockPanel を使用せずに。)
次のコードは、問題を示しています。
<Window x:Class="ExampleApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:loc="clr-namespace:ExampleApplication3"
Title="Example" Height="600" Width="600">
<Grid Background="LightGray">
<loc:UserControl1 />
</Grid>
</Window>
次の定義はUserControl1
、設計時には適切に表示されますが、実行時には固定サイズで表示されます。
<UserControl x:Class="ExampleApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Grid Background="LightCyan" />
</UserControl>
次の の定義はUserControl1
、設計時にはドットとして表示されますが、Window1
実行時には親を埋めるように展開されます。
<UserControl x:Class="ExampleApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid Background="LightCyan" />
</UserControl>