15

I am trying to figure out how to get a textbox to wrap its contents, however the situation isn't quite the same as the typical "it doesn't wrap" scenario. My textbox is contained inside a DataTemplate which is used inside a Telerik RadTabControl instance (using a ContentTemplatePresenter to determine which view to display) and the XAML for the DataTemplate looks like this:

<DataTemplate x:Key="NotesTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBlock Text="Use the box below to record any general notes associated with this item." Style="{StaticResource Default}" />
        <TextBox TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" GridRow="1" Margin="20" Text="{Binding Notes, UpdateSourceTrigger=PropertyChanged}" />
     </Grid>
</DataTemplate>

The reason I say it doesn't fall under the normal "it doesn't wrap" scenario is it used to wrap until I had to change the view to be resizable to anything to support the varying screen sizes the app will be run on. When I did that the TextBox stopped wrapping because (presumably) as the user types something the TextBox says "I need more space" so the parent obliges and the box continues out to the right indefinitely (although the view gets scrollbars). I tried setting a MaxWidth using Binding/RelativeSource, but since the parent is specifically designed to grow that approach won't work. What I need to have happen is the box should be the width of its' containing parents' VisibleWidth. Meaning, if the Window itself is 1024x768, the TextBox's MaxWidth should be 1024 and any text thereafter would automatically wrap, but if the Window grows to 1280x1024 the box should now be 1280 and the text wrap accordingly. I tried this scenario with this binding expression, but no luck:

MaxWidth="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=ActualWidth}"

The Window size itself isn't growing so if I could get the Window's Width (minus a certain amount to cover the width of the tabs that are part of the TabControl) I believe that would work.

Any ideas?

Scott Salyer
  • 2,165
  • 7
  • 45
  • 82
  • Ended up finding the best result via another SO question: http://stackoverflow.com/questions/386039/wpf-textbox-and-scroll-behavior. – Scott Salyer Oct 19 '11 at 22:21

1 Answers1

19

although the view gets scrollbars

Disable the horizontal scrollView, so it will be forced to wrap. You can try to disable it on the TextBox itself, or on the wrapping Grid.

<DataTemplate x:Key="NotesTemplate">
    <Grid ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBlock Text="Use the box below to record any general notes associated with this item." Style="{StaticResource Default}" />
        <TextBox TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" Grid.Row="1" Margin="20" Text="{Binding Notes, UpdateSourceTrigger=PropertyChanged}" />
     </Grid>
</DataTemplate>
MichaelS
  • 7,023
  • 10
  • 51
  • 75
  • 1
    That didn't work, but this SO link got me 99% of the way there: http://stackoverflow.com/questions/386039/wpf-textbox-and-scroll-behavior. The only thing I don't know about is if they resize the window back to a smaller resolution the box retains the original width, but I'm not sure if that matters to me or not and I'm leaning towards not. – Scott Salyer Oct 19 '11 at 22:20
  • @digitall: I just ran the code here and it worked fine (just the grid). Maybe the other wrapping controls messing it up. – MichaelS Oct 19 '11 at 22:29
  • In a grid with 8 columns (which have one column set to auto), a textbox with a columnspan="8", the wrap doesn't work, the textbox increase its size automatically ;( – lunair Mar 26 '15 at 14:56
  • In case anyone is wondering, this fix works also when the scrolbar is created by a ListBox instead of a DataGrid. Or at least it worked for me. – Adrian Ratnapala Aug 08 '15 at 07:15