How to make a textbox stretch to its parent window's width in C#?
I have a TextBox
which I would like to have stretch to its开发者_如何学C parent's Width
.
I tried anchoring Top, Left, Right but that only goes just over about 60%, any ideas?
As Hans Passant said in a comment, but anchor to the left as well.
Or, if you want to do it programmatically:
textBox1.Width = textBox1.Parent.Width;
textBox1.Location = new Point(0,textBox1.Location.Y);
textBox1.Anchor = AnchorStyles.Left | AnchorStyles.Right;
In WPF use Binding on the property ActualWidth
of the parent control (in this case its named Container):
Width="{Binding Container, Path=ActualWidth}"
Another possibility is this (if you don't have named your control and just want to bind to the parent control):
Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}"
In Windows Store, Windows Phone 8.0 and 8.1, WPF and all XAML based UI platforms it's possible to use properties
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
精彩评论