When creating UserControl in WPF, itโs more convenient for me to give some arbitrary Height and Width values โโso that I can view my changes in the Visual Studio designer. However, when I launch the control, I want the height and width to be undefined, so the control will expand to fill any container that I put it in. How can I get the same functionality without removing the height and width of the values โโbefore building my control? (Or without using the DockPanel in the parent container.)
The following code demonstrates the problem:
<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>
The following UserControl1 definition is displayed at design time, but displayed as a fixed size at run time:
<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>
The following UserControl1 definition appears as a dot at design time, but expands to populate parent Window1 at run time:
<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>
wpf user-controls autosize
Joseph Sturtevant Sep 16 '08 at 18:32 2008-09-16 18:32
source share