Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Have two controls fill the MainWindow and resize automatically

Have two controls fill the MainWindow and resize automatically

Scheduled Pinned Locked Moved WPF
tutorialdockerquestion
3 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    I have four controls in a horizontal row. The first and third control shouöd automatically resize and fill the available space when resizing the MainWindow. The second and forth controls have a static width and I don't want them to change horizontally in size. All four controls should fill vertically. I have almost solved it with an UniformGrid as container for the four controls.

    Run the example and resize the window. * The blue and red rectangle will resize appropriately horizontally and fill vertically. * The green and yellow rectangle only fills vertically. But I cannot understand how to fill/remove the white spaces to the left of all rectangles. Any suggestions?

    Richard DeemingR 1 Reply Last reply
    0
    • M Mc_Topaz

      I have four controls in a horizontal row. The first and third control shouöd automatically resize and fill the available space when resizing the MainWindow. The second and forth controls have a static width and I don't want them to change horizontally in size. All four controls should fill vertically. I have almost solved it with an UniformGrid as container for the four controls.

      Run the example and resize the window. * The blue and red rectangle will resize appropriately horizontally and fill vertically. * The green and yellow rectangle only fills vertically. But I cannot understand how to fill/remove the white spaces to the left of all rectangles. Any suggestions?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The columns in a UniformGrid will always be the same width. Since you have four columns, each column will be ¼ of the width of the window. The controls with a defined width are aligned in the centre of the column, which is why you're getting white-space around them. The obvious option would be to use a standard Grid. This would require adding column definitions, and setting the Grid.Column attached property on each child control.

      <Grid>
      <Grid.ColumnDefinitions>
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="Auto" />
      <ColumnDefinition Width="*" />
      <ColumnDefinition Width="Auto" />
      </Grid.ColumnDefinitions>

      <Rectangle Fill="Blue" Grid.Column="0" />
      <Rectangle Fill="Lime" Width="20" Grid.Column="1" />
      <Rectangle Fill="Red" Grid.Column="2" />
      <Rectangle Fill="Yellow" Width="20" Grid.Column="3" />
      

      </Grid>

      Another alternative would be to combine the UniformGrid with a DockPanel:

      <UniformGrid Columns="2">
      <DockPanel>
      <Rectangle Fill="Lime" Width="20" DockPanel.Dock="Right" />
      <Rectangle Fill="Blue" />
      </DockPanel>
      <DockPanel>
      <Rectangle Fill="Yellow" Width="20" DockPanel.Dock="Right" />
      <Rectangle Fill="Red" />
      </DockPanel>
      </UniformGrid>


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        The columns in a UniformGrid will always be the same width. Since you have four columns, each column will be ¼ of the width of the window. The controls with a defined width are aligned in the centre of the column, which is why you're getting white-space around them. The obvious option would be to use a standard Grid. This would require adding column definitions, and setting the Grid.Column attached property on each child control.

        <Grid>
        <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>

        <Rectangle Fill="Blue" Grid.Column="0" />
        <Rectangle Fill="Lime" Width="20" Grid.Column="1" />
        <Rectangle Fill="Red" Grid.Column="2" />
        <Rectangle Fill="Yellow" Width="20" Grid.Column="3" />
        

        </Grid>

        Another alternative would be to combine the UniformGrid with a DockPanel:

        <UniformGrid Columns="2">
        <DockPanel>
        <Rectangle Fill="Lime" Width="20" DockPanel.Dock="Right" />
        <Rectangle Fill="Blue" />
        </DockPanel>
        <DockPanel>
        <Rectangle Fill="Yellow" Width="20" DockPanel.Dock="Right" />
        <Rectangle Fill="Red" />
        </DockPanel>
        </UniformGrid>


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        M Offline
        M Offline
        Mc_Topaz
        wrote on last edited by
        #3

        Of course a standard Grid is the obvious solution. Thank you for showing me! The UniFormGrid combined with a DockPanel was a neat solution as well.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups