5

I am working on a MVVM app (the examples here are not very MVVM though, its just a mockup).

I have a ListView, that binds to a collection of TextItemViewModel instances, the view for these are defined in TextItemView. Each TextItemView contains a Grid with one Auto column and on * column to fill. The fill column contains a TextBox where the user can enter text.

I want the TextItemViews to fill the horizontal space of the ListView (this works), like so:

enter image description here

However, when a user enters long text in a box, I want it to expand in Height but not in Width. At the moment, the Width increases and the ListView gets a scrollbar, like so:

enter image description here

My ListView code looks like this: (I've taken out the MVVM Binding code and set the ItemsSource in the code-behind for this example)

<ListView Name="listview"
          SelectionMode="Single"
          Margin="5"
          MinHeight="50"
          HorizontalAlignment="Stretch"
          HorizontalContentAlignment="Stretch" />

And my TextItemView code looks like this:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <Button Content="SomeButton"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"                
            Margin="5"
            Grid.Column="0" />

    <TextBox Text="{Binding Path=NoteText}"
             TextWrapping="Wrap"
             AcceptsReturn="True"
             AcceptsTab="True"
             VerticalScrollBarVisibility="Auto"
             HorizontalScrollBarVisibility="Disabled"
             HorizontalAlignment="Stretch"                 
             Margin="5"                 
             Grid.Column="1" />
</Grid>

Can anyone advise me on how to force the TextItemView to fill the width of the ListView (as it does), but to then wrap the TextBox and expand TextItemView in Height when the TextBox fills up?

Pieter Müller
  • 4,573
  • 6
  • 38
  • 54

1 Answers1

13

OK I got this now thanks to WPF - How to stop TextBox from autosizing?

Trick is to add the following to ListView definition:

ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Community
  • 1
  • 1
Pieter Müller
  • 4,573
  • 6
  • 38
  • 54