Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listbox item that stretches to width of listbox BUT not past the width

Tags:

c#

.net

wpf

I have a listbox that I want to take up the width of my window, and the list box items will stretch to the size of the listbox.

Each listbox item will be a datatemplate that has about 150 width for information and the remaining size to be a textbox for a description. I want the description texbox to stretch to remaining available size. So here is example xaml that I thought would create this layout:

<Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
  <ListBox Margin="20,20,20,20" ItemsSource="{Binding Path=List}">
     <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
           <Setter Property="HorizontalContentAlignment"
                   Value="Stretch" />
        </Style>
     </ListBox.ItemContainerStyle>
     <ListBox.ItemTemplate>
        <DataTemplate>
           <Border BorderBrush="AliceBlue"
                   BorderThickness="1">
              <Grid>
              <Grid.ColumnDefinitions>
                 <ColumnDefinition Width="150" />
                 <ColumnDefinition Width="*" />
              </Grid.ColumnDefinitions>
                 <Label Width="20"
                        Content="Test" />
                 <ComboBox Width="130" />
                 <TextBox Grid.Column="1"
                          HorizontalAlignment="Stretch"
                          HorizontalContentAlignment="Stretch" />
              </Grid>
           </Border>
        </DataTemplate>
     </ListBox.ItemTemplate>
  </ListBox>

However, when I type in the textbox and the text width goes past the width of the listbox, the listbox item keeps growing and the horizontal scroll bar shows up.

What I want to achieve is the max width of the textbox to go just up to the listbox and not grow wider. Anyone know how I can achieve this layout?

like image 374
cab Avatar asked Sep 21 '11 13:09

cab


1 Answers

Try adding ScrollViewer.HorizontalScrollBarVisibility="Disabled" to ListBox, it worked for me.

like image 153
Dmitry Avatar answered Oct 06 '22 09:10

Dmitry