Dynamic Controls not Stretching to Container Width
-
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
-
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
Have you tried setting the ContentControl's HorizontalContentAligment to Stretch?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Have you tried setting the ContentControl's HorizontalContentAligment to Stretch?
Mark Salsbery Microsoft MVP - Visual C++ :java:
Worked perfectly, thank you!
Jeremy Likness Latest Article: Decoupled ChildWindow Dialogs in Silverlight with Prism Blog: C#er : IMage