Answered by:
Max width on textbox

Question
-
Hi
Im having this problem that a textbox insida a listbox wont get 100% in width.
This is my structure:
<ListBox Name="listBoxList" Width="auto" IsSynchronizedWithCurrentItem="True" Grid.Row="1" ScrollViewer.VerticalScrollBarVisibility="Disabled"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical" Width="auto"> <TextBox x:Name="txtAsd" Text="asd" AcceptsReturn="True" Width="auto" /> <Button x:Name="btnSave" Content="Save" Click="btnSave_Click" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
I tried putting Width="{Binding ElementName=listBoxList, Path=ActualWidth}" on the stackpanel but that makes it abit too wide..
How do I get 100% width on the textbox?Saturday, October 31, 2009 10:53 PM
Answers
-
Hi hbarck,I think it will stretch if you set the HorizontalContentAlignment property of the ListBox to Stretch. You may still have to use a style for ListBoxItem to override the default padding of 2,0,0,0 if you want it to really fit without a small space on the left.hth,Marcel
http://dutchmarcel.wordpress.com/- Proposed as answer by hbarck Monday, November 2, 2009 12:14 PM
- Marked as answer by Bruce.Zhou Wednesday, November 4, 2009 7:47 AM
Monday, November 2, 2009 11:57 AM
All replies
-
You cant use Auto as Width for this... The best thing is to set this in hard code....
KennethSaturday, October 31, 2009 11:13 PM -
But all controls is set to auto. But still the textbox wont get with 100%Saturday, October 31, 2009 11:33 PM
-
Well... If I remember correcly the ListBox and the ListView dosent respond to well to that...
KennethSaturday, October 31, 2009 11:36 PM -
Hi,
the problem is that the ControlTemplate for ListBoxItem contains a Border that does not stretch to the full width of the control. You can make it do that by replacing the ControlTemplate for ListBoxItem with a modified template that tells the border to stretch:
<Style x:Key="{x:Type ListBoxItem}" TargetType="ListBoxItem"> <Setter Property="SnapsToDevicePixels" Value="true"/> <Setter Property="OverridesDefaultStyle" Value="true"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Border Name="Border" Padding="2" SnapsToDevicePixels="true" HorizontalAlignment="Stretch"> <ContentPresenter /> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style>
However, notice that I left out some bits in this example, so you might want to look up the original ControlTemplate for ListBoxItem and modify it to make it work for what you need.Sunday, November 1, 2009 11:16 AM -
Hi hbarck,I think it will stretch if you set the HorizontalContentAlignment property of the ListBox to Stretch. You may still have to use a style for ListBoxItem to override the default padding of 2,0,0,0 if you want it to really fit without a small space on the left.hth,Marcel
http://dutchmarcel.wordpress.com/- Proposed as answer by hbarck Monday, November 2, 2009 12:14 PM
- Marked as answer by Bruce.Zhou Wednesday, November 4, 2009 7:47 AM
Monday, November 2, 2009 11:57 AM -
Hi Marcel,
you are right, this is much more elegant...Monday, November 2, 2009 12:14 PM