Sunday, 28 July 2013

WPF - How to set width to 100 percent

In WPF if you want to set the width of a UI element to 100%, its a bit different from how you do in HTML. The width cannot be set like width="100%".
As we want the element to take all the width available for it within its parent container we will stretch the control to take all the available space within the parent container. For this we will set the "HorizontalContentAlignment" property of the control to "Stretch"
For example if you want a TextBox to change its width dynamically according to the parent container's width set:
<Textbox HorizontalContentAlignment="Stretch"/>

No comments:

Post a comment