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. Dynamic Controls not Stretching to Container Width

Dynamic Controls not Stretching to Container Width

Scheduled Pinned Locked Moved WPF
csharpcsswpfwcfcom
3 Posts 2 Posters 0 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.
  • J Offline
    J Offline
    Jeremy Likness
    wrote on last edited by
    #1

    Imagine an ItemsControl that is a fixed width. Each item is a grid with a fixed width as well. Let's say each grid item is 200 pixels wide, so when generated in the items control it's like: Name: Value Name: Value Name: Value Value is dynamically rendered. I host it inside a content control, like this:

    <ContentControl Content="{Binding Converter={ControlConverter}}" HorizontalAlignment="Stretch"/>

    Inside the converter, I might return a TextBox, a PasswordBox, a CheckBox, etc. The problem I'm having is that I am setting the width of these like so:

    return new TextBox() { HorizontalAlignment=HorizontalAlignment.Stretch };

    Of course I'm setting other properties but you get the gist. Unfortunately, the resulting controls are sizing themselves to the content, not the container. The stretch basically isn't working the same as if I have a actual TextBox in place of the content control. Anyone have an idea of why the stretch isn't working? Should I wire into some event and rebind the size somehow or make it dependent on the parent control (perhaps pass the parent as a parameter?) Thanks, Jeremy

    Jeremy Likness Latest Article: Decoupled ChildWindow Dialogs in Silverlight with Prism Blog: C#er : IMage

    M 1 Reply Last reply
    0
    • J Jeremy Likness

      Imagine an ItemsControl that is a fixed width. Each item is a grid with a fixed width as well. Let's say each grid item is 200 pixels wide, so when generated in the items control it's like: Name: Value Name: Value Name: Value Value is dynamically rendered. I host it inside a content control, like this:

      <ContentControl Content="{Binding Converter={ControlConverter}}" HorizontalAlignment="Stretch"/>

      Inside the converter, I might return a TextBox, a PasswordBox, a CheckBox, etc. The problem I'm having is that I am setting the width of these like so:

      return new TextBox() { HorizontalAlignment=HorizontalAlignment.Stretch };

      Of course I'm setting other properties but you get the gist. Unfortunately, the resulting controls are sizing themselves to the content, not the container. The stretch basically isn't working the same as if I have a actual TextBox in place of the content control. Anyone have an idea of why the stretch isn't working? Should I wire into some event and rebind the size somehow or make it dependent on the parent control (perhaps pass the parent as a parameter?) Thanks, Jeremy

      Jeremy Likness Latest Article: Decoupled ChildWindow Dialogs in Silverlight with Prism Blog: C#er : IMage

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Have you tried setting the ContentControl's HorizontalContentAligment to Stretch?

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      J 1 Reply Last reply
      0
      • M Mark Salsbery

        Have you tried setting the ContentControl's HorizontalContentAligment to Stretch?

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        J Offline
        J Offline
        Jeremy Likness
        wrote on last edited by
        #3

        Worked perfectly, thank you!

        Jeremy Likness Latest Article: Decoupled ChildWindow Dialogs in Silverlight with Prism Blog: C#er : IMage

        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