Search code examples
wpfuser-controlsautosize

AutoSizing of Usercontrol


For a WPF application I have a user control MyUsrCntrl with Height=300 and Width=300 When i place MyUsrCntrl in a window and set its size to 600x600 the user control get resized but the controls in that does not get resized , is there any solution for this.


Solution

  • If you want to extend and compress the height and Width of your Usercontrol then make the Parent Controls Height="*" and Width="*" and don't assign any height and width to your controls present inside UserControl. Something like:

    <Grid Margin="4" Background="Orange">    
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="*"/>    
        </Grid.ColumnDefinitions>    
        <StackPanel Grid.Column="0">    
            <TextBlock Text="It is fixed" Margin="1"/>       
            <Button Content="It is fixed"/>    
        </StackPanel>    
        <StackPanel Grid.Column="1">    
            <TextBlock Text="It is variable" Margin="1"/>    
            <Button Content="It is fixed" Margin="2"/>    
        </StackPanel>    
    </Grid>