locked
Urgent need to resize controls based on size of windows RRS feed

  • Question

  • Hi,

    I am developing applicationin WPF and I want to know how I can create styles dynamically and apply it to controls? i need to do this because I want my controls to resize according to window size.

    Kindly help. Do not want to miss deadline.

    Wednesday, July 13, 2011 11:55 AM

Answers

  • Hi Ajay Parsana,

    You can use DynamicResource to set the size of the controls in their style, and then you can easily modified the corresponding resources to change the size of the controls.

    However, I think a better solution is to relatively layout your application.

    Here is a simple sample.

        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="1.5*"/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition />
            <RowDefinition/>
          </Grid.RowDefinitions>
          <TextBlock Grid.Column="0" Grid.Row="0" Text="Name"/>
          <TextBlock Grid.Column="0" Grid.Row="1" Text="Age"/>
          <TextBlock Grid.Column="0" Grid.Row="2" Text="Grade"/>
          <TextBox Grid.Column="1" Grid.Row="0" Text="Peter"/>
          <TextBox Grid.Column="1" Grid.Row="1" Text="16"/>
          <TextBox Grid.Column="1" Grid.Row="2" Text="A"/>
          <Button Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Content="OK" HorizontalAlignment="Stretch"/>
        </Grid>
    

    Please see Panels Overview and How to Layout a WPF Application for more information.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Ajay Parsana Friday, July 15, 2011 7:05 AM
    Friday, July 15, 2011 6:09 AM

All replies

  • You must elaborate on this issue.
    What controls are you trying to fit in what parent control?

    Or are you just talking about fitting a grid to the size of the Window it is in?

    Useally setting the HorizontalAlignment & VerticalAlignment to Stretch, or the Height & Width to Auto does the trick.

    If these are not of any help to you. Please elaborate on the subject. 


    Developing is part of being a developer.
    Wednesday, July 13, 2011 12:04 PM
  • Hey Ajay,

    If you're using a grid, a stackpanel, a dockpanel, a wrapPanel, or any kind of containers as a main container just do not specify width or height and the container will stretch itself to the window. If my answer wasn't clear enough just provide us with code snippets (XAML and C#).

    Please, mark my reply as "Answer" if it helps or vote it up if it gives a key to the solution.

    Thanks in advance.


    Ahmed M. Gamil

    "It is not titles that honor men, but men that honor titles."

    Wednesday, July 13, 2011 12:21 PM
  • Hi All,

     

    First of Thanks.

     

    Basically I am using two Stack Panels in a single Window placed vertically aside.In one I have Tree view and In second I have Buttons. My application to have Textbox and Labels similar to user forms. 

    I want to make this application scalable to any screen from 15 inch to 21 inch....

    Is there any way I can dynamically create Style for each controls such as Button,Label,textbox and apply it to them based on screen size.

    Thanks in advance.

     

    Thursday, July 14, 2011 8:39 AM
  • Hi All,

     

    First of Thanks.

     

    Basically I am using two Stack Panels in a single Window placed vertically aside.In one I have Tree view and In second I have Buttons. My application to have Textbox and Labels similar to user forms. 

    I want to make this application scalable to any screen from 15 inch to 21 inch....

    Is there any way I can dynamically create Style for each controls such as Button,Label,textbox and apply it to them based on screen size.

    Thanks in advance.

    Thursday, July 14, 2011 8:39 AM
  • Hi All,

     

    First of Thanks.

     

    Basically I am using two Stack Panels in a single Window placed vertically aside.In one I have Tree view and In second I have Buttons. My application to have Textbox and Labels similar to user forms. 

    I want to make this application scalable to any screen from 15 inch to 21 inch....

    Is there any way I can dynamically create Style for each controls such as Button,Label,textbox and apply it to them based on screen size.

    Thanks in advance.

    Thursday, July 14, 2011 8:39 AM
  • Hi Ajay Parsana,

    You can use DynamicResource to set the size of the controls in their style, and then you can easily modified the corresponding resources to change the size of the controls.

    However, I think a better solution is to relatively layout your application.

    Here is a simple sample.

        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="1.5*"/>
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition />
            <RowDefinition/>
          </Grid.RowDefinitions>
          <TextBlock Grid.Column="0" Grid.Row="0" Text="Name"/>
          <TextBlock Grid.Column="0" Grid.Row="1" Text="Age"/>
          <TextBlock Grid.Column="0" Grid.Row="2" Text="Grade"/>
          <TextBox Grid.Column="1" Grid.Row="0" Text="Peter"/>
          <TextBox Grid.Column="1" Grid.Row="1" Text="16"/>
          <TextBox Grid.Column="1" Grid.Row="2" Text="A"/>
          <Button Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Content="OK" HorizontalAlignment="Stretch"/>
        </Grid>
    

    Please see Panels Overview and How to Layout a WPF Application for more information.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Ajay Parsana Friday, July 15, 2011 7:05 AM
    Friday, July 15, 2011 6:09 AM