1

I have ListBox with HorizontalAlignment="Left" so listbox width depends on it's content. There is a text box in each listbox item. When I typing into text box the width of listbox increasing. I want that TextBox width depends on the ListBox item width but not vice versa and text in the TextBox is wrapped.

<Window x:Class="WpfOverflow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="1000">
<ListBox Background="CadetBlue" HorizontalAlignment="Left" MaxWidth="500">
  <ListBox.Items>
    <ListBoxItem>
      <DockPanel>
        <TextBlock Text="Some text of the element" DockPanel.Dock="Top"/>
        <TextBox Text="Enter text here" TextWrapping="WrapWithOverflow"/>
      </DockPanel>
    </ListBoxItem>
  </ListBox.Items>
</ListBox>

Initial width of the list box ListBox width grows on typing

UPDATE: Not accepted answer but answer by @bathineni helped in this case Prevent a TextBox from horizontal expanding in WPF

<ListBox Background="CadetBlue" HorizontalAlignment="Left" MaxWidth="500" HorizontalContentAlignment="Stretch">
  <ListBox.Items>
    <ListBoxItem>
      <DockPanel>
        <TextBlock Text="==== Some text of the element =====" DockPanel.Dock="Top"/>
        <ScrollViewer  HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Disabled" Name="scv" >
          <TextBox Text="Enter text here" Style="{StaticResource textBoxMultiline}" MaxWidth="{Binding ElementName=scv, Path=ActualWidth}" HorizontalAlignment="Stretch" />
        </ScrollViewer>
      </DockPanel>
    </ListBoxItem>
  </ListBox.Items>
</ListBox>
2
  • Is this not a case of simply defining a Width on your ListBoxItem?
    – Mike Eason
    Nov 3, 2015 at 9:01
  • I don't want to fix a width, width depends on elements in the top part of list box item represented by "Some text of the element" this part width depends on user data.
    – Ed Pavlov
    Nov 3, 2015 at 9:10

2 Answers 2

0
<ListBox Background="CadetBlue" HorizontalAlignment="Left" MaxWidth="500" Width="200"  >
        <ListBox.Items>
            <ListBoxItem>
                <DockPanel>
                    <TextBlock Text="Some text of the element" DockPanel.Dock="Top"/>
                    <TextBox Text="Enter text here" TextWrapping="NoWrap" Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, Path=ActualWidth}" />
                </DockPanel>
            </ListBoxItem>
        </ListBox.Items>
    </ListBox>
1
  • This solution without text wrapping also works well
    – Ed Pavlov
    Nov 3, 2015 at 11:07
0

If you want to achieve this:

example wpf image

Simply put MaxWidth on your ListBoxItem.

<ListBox Background="CadetBlue" HorizontalAlignment="Left">
        <ListBox.Items>
            <ListBoxItem MaxWidth="150"> 
                <DockPanel>
                    <TextBlock Text="Some text of the element" DockPanel.Dock="Top"/>
                    <TextBox Text="Enter text here" TextWrapping="WrapWithOverflow"/>
                </DockPanel>
            </ListBoxItem>
        </ListBox.Items>
    </ListBox>
1
  • I don't want to fix a width, width depends on elements in the top part of list box item represented by "Some text of the element" this part width depends on user data. If user data less than 150pt, then it will grow to 150pt.
    – Ed Pavlov
    Nov 3, 2015 at 9:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.