Parental Control ScrollViewer instead of ScrollViewer child control

I have it:

<Window x:Class="ScrollTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="525"> <ScrollViewer ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <GroupBox Grid.Row="0" Header="Stuff" Height="200"> <TextBlock Text="Lots of controls go here" HorizontalAlignment="Center" VerticalAlignment="Center" /> </GroupBox> <TabControl Grid.Row="1"> <TabItem Header="Main Tab"> <TextBox MinHeight="100" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" AcceptsReturn="True" /> </TabItem> </TabControl> </Grid> </ScrollViewer> </Window> 

When I add too many lines to a TextBox , instead of the ScrollViewer used by the TextBox used, the field is stretched and an external ScrollViewer . Can I prevent this without fixing the height of the TextBox or TabControl ?

Update:

If I MinHeight in a TextBox and set MaxLines to 5, this is what I get:

MinHeight removed and MaxLines set to 5

If I added the 6th row, the TextBox ScrollViewer scrollbars are ScrollViewer , but they still remain vertically centered in the TextBox control.

+6
source share
3 answers

I managed to get close to this:

 <Window x:Class="ScrollTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="525"> <ScrollViewer ScrollViewer.VerticalScrollBarVisibility="Visible" x:Name="Base"> <Grid Height="{Binding ElementName=Base, Path=ActualHeight, Mode=OneWay}" MinHeight="400"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <GroupBox Grid.Row="0" Header="Stuff" Height="200"> <TextBlock Text="Lots of controls go here" HorizontalAlignment="Center" VerticalAlignment="Center" /> </GroupBox> <TabControl Grid.Row="1"> <TabItem Header="Main Tab"> <Grid x:Name="myInnerGrid"> <TextBox MinHeight="100" MaxHeight="{Binding ElementName=myInnerGrid, Path=ActualHeight, Mode=OneWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Left" VerticalContentAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" AcceptsReturn="True" /> </Grid> </TabItem> </TabControl> </Grid> </ScrollViewer> </Window> 

Notice the height snap expression for the outer grid and MaxHeight for the TextBox .

This is still not ideal, because you need to manually install MinHeight , which will trigger an external large scrollbar. It is likely close to WPF if it does not write a new grid control.

The idea was found here: http://social.msdn.microsoft.com/Forums/en/wpf/thread/7b4b0c88-6b8f-4f07-aa8b-8e7018762388

+1
source

Try looking at MaxLines and MinLines Properties.

Top link:

Setting this property changes the size of the text field if the number of visible lines exceeds the limit specified by MaxLines. This property applies only to visible lines and does not limit the number of lines. Depending on its configuration, the text field may contain additional invisible lines available for scrolling. If the Height property is explicitly set in the TextBox, MaxLines and MinLines property values โ€‹โ€‹are ignored.

Try changing:

 <TextBox MinHeight="100" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ... 

to

 <TextBox MinLines="5" MaxLines="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 

Edit: Try it. It sets the VerticalContentAlignment for the TabItem . This will save the text box at the top of the Tab , I also set maximum limits on what your available area can hold, if you change the size of your form, you can configure this number to use all available space.

 <TabItem Header="Main Tab" VerticalContentAlignment="Top" > <TextBox ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible" MinLines="8" MaxLines="8" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" AcceptsReturn="True" /> </TabItem> 

Edit:

Further, the reason the scrollbars are not displayed on the TextBox is because TabControl and TabItem resized to TextBox . You need to make the maximum height set either to TabControl , TabItem , or TextBox , which will allow ScrollViewer to work for TextBox .

+1
source

I found that the best solution for this is to use the Border trick indicated here , applied both vertically and horizontally.

In the following example, a ScrollViewer contains a TextBox , where it is desirable for the TextBox fit all available spaces (vertically and horizontally) and scroll it vertically to the parent ScrollViewer . Border PlaceHolderBorder controls the Width TextBox es when the parent window is resized. Border DescriptionPlaceHolderBorder controls the Height Description TextBox as the size of the parent control changes, and the ScrollViewer from the TextBox fires before the parent control.

It is important to have Margin in Border s placeholder.

 <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Background="{StaticResource ControlBackgroundBrush}"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="*"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="10"></ColumnDefinition> <ColumnDefinition Width="Auto"></ColumnDefinition> <ColumnDefinition Width="*"></ColumnDefinition> <ColumnDefinition Width="10"></ColumnDefinition> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Style="{DynamicResource LabelHeader}" Content="Company" /> <Label Grid.Row="1" Grid.Column="1" Style="{DynamicResource CompanyNameInput}" Content="{Binding CompanyNameLabel}" /> <Label Grid.Row="2" Grid.Column="1" Style="{DynamicResource DescriptionInput}" Content="{Binding DescriptionLabel}" /> <Border Name="PlaceHolderBorder" Grid.Column="2" Margin="7"/> <TextBox Grid.Row="1" Grid.Column="2" Text="{Binding CompanyName}" MaxLength="255"/> <Border Name="DescriptionPlaceHolderBorder" Grid.Row="2" Margin="7"/> <TextBox Grid.Row="2" Grid.Column="2" Text="{Binding Description}" VerticalScrollBarVisibility="Auto" TextAlignment="Left" TextWrapping="Wrap" AcceptsReturn="True" MinHeight="60" Width="{Binding ElementName=PlaceHolderBorder, Path=ActualWidth}" Height="{Binding ElementName=DescriptionPlaceHolderBorder, Path=ActualHeight}" /> <StackPanel Orientation="Horizontal" Grid.Row="3" Grid.Column="2" Margin="5"> <Button Command="{Binding UpdateCommand}" Content="{Binding UpdateButtonLabel}"></Button> <Button Command="{Binding ResetCommand}" Content="{Binding ResetButtonLabel}"></Button> <Button Command="{Binding CloseConfirmCommand}" Content="{Binding CloseButtonLabel}"></Button> </StackPanel> </Grid> </ScrollViewer> 
0
source

Source: https://habr.com/ru/post/904861/


All Articles