Usher
Usher

Reputation: 2146

doesn't wrap the text in the textbox in WPF

enter image description hereGot stuck up with wrap the text when user enters in the text box,used "MaxWidth to control the text box and wrap the text but it shrink the textbox size.am not sure how i have to strecth it,eventhoug am using HorizantalAlignment ="Stretch" but it doesn't match with the other text box.

If i don't wrap the text then the whole grid is moving to if the text is larger than the text box,is this anyway to fix the size and wrap the text please.

Here is my xaml code

  <DockPanel Grid.Row="1">
            <Grid Name="gLeftContent" Grid.Row="0" Background="Transparent" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30*"/>
                    <ColumnDefinition Width="30*"/>
                    <ColumnDefinition Width="30*"/>
                    <ColumnDefinition Width="10*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="30" />
                    <RowDefinition Height="20"/>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="0"/>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="0" />
                    <RowDefinition Height="25" />
                    <!-- Row 14 -->
                    <RowDefinition Height="25" />
                </Grid.RowDefinitions>

                <TextBox 
                Name="tbName" 
                Text="{Binding SelectedStory.Slug, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" 
                Width="{Binding ActualWidth, ElementName=tbName}"   **//used ActualWidth but it shrinks the text box and doesn't match with the other text box.**
                Grid.Column="1" 
                Grid.Row="5" 
                TextWrapping="Wrap" 
                d:LayoutOverrides="Height" 
                Grid.ColumnSpan="2" 
                HorizontalAlignment="Stretch" 
                LostFocus="tbSlug_LostFocus"
                >
                 <TextBox 
                Name="tbHeadline" 
                Text="{Binding SelectedStory.Headline, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" 
                Grid.Column="1"  
                Grid.Row="6" 
                TextWrapping="Wrap" 
                d:LayoutOverrides="Height" 
                Grid.ColumnSpan="2" 
                HorizontalAlignment="Stretch" 
                LostFocus="tbHeadline_LostFocus" 
                 />
                </TextBox>
                <ItemsControl 
                Grid.Column="1" 
                Grid.Row="14" 
                Grid.RowSpan="2" 
                Grid.ColumnSpan="2" 
                cal:RegionManager.RegionName="{x:Static inf:RegionNames.MetadataGenerateImagesRegion}"/>
            </Grid>
        </DockPanel>

Thanks in Advance.

Upvotes: 1

Views: 3801

Answers (3)

paparazzo
paparazzo

Reputation: 45106

You need to set the RowDefinition Height="Auto" do it can grow to fit additional lines.

Definitely should not be binding width to itself.

Try

 <TextBox 
            Name="tbName" 
            Text="{Binding SelectedStory.Slug, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}" 
            Grid.Column="1" 
            Grid.Row="5"  
            Grid.ColumnSpan="2" 
            TextWrapping="Wrap"
            HorizontalAlignment="Stretch"
            LostFocus="tbSlug_LostFocus" />

Upvotes: 1

user694833
user694833

Reputation:

Let's start again with a clean answer:

        <TextBox Name="tbName"  
        Text="{Binding SelectedStory.Slug, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True, ValidatesOnDataErrors=True}" Validation.ErrorTemplate="{StaticResource ErrorTemplate}"  
        Width="Auto" Height="Auto"
        HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"
        Grid.Column="1"  Grid.Row="5"  Grid.ColumnSpan="2"
        TextWrapping="Wrap"  AcceptsReturn="True"  
        LostFocus="tbSlug_LostFocus" > 

Please replace it entirely in your code to avoid any omision.

Upvotes: 1

user694833
user694833

Reputation:

Try to add to your textbox:

AcceptsReturn="True"

Although your control may present wrapped text, currently it is unable to accept enter keystrokes.

Upvotes: 0

Related Questions