c# - How to prevent outer scrollviewer from overriding inner scrollviewer?
问题描述
I have the following section within my Grid:
<ScrollViewer Grid.Column="4" Grid.Row="0" CanContentScroll="True" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical" Margin="10" HorizontalAlignment="Stretch">
<!-- other controls such as sliders, Textboxes and so on -->
<Border Height="200" BorderThickness="1" BorderBrush="DarkGray" Margin="5" HorizontalAlignment="Stretch">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="auto" >
<TextBlock Text="{Binding LogOutput}" Style="{StaticResource BodyTextBlockStyle}" TextWrapping="NoWrap"/>
</ScrollViewer>
</Border>
</StackPanel>
</ScrollViewer>
If I add a very long text into the LogOuput
(tested with "Ipsum Lorem"), for some reason the width of my border increases and the outer ScrollViewer
is showing a horizontal scrollbar.
What I would like to have, is that the width of the border doesn't change (always stretching to the available space of this grid column) and the inner ScrollViewer
should show the horizontal scrollbar.
I know I could achieve this, by setting a fixed width to the border, however - as mentioned - I want the border to stretch to the available space.
How can I achieve this?
解决方案
You could bind the Width
of inner ScrallViewer
to the width of column:
<Border Height="200" BorderThickness="1" BorderBrush="DarkGray" Margin="5" HorizontalAlignment="Stretch"
Width="{Binding Path=ColumnDefinitions[putColIndexHere], Converter={StaticResource ColDefinitionToWidthConverter}, ConverterParameter=30, RelativeSource={RelativeSource AncestorType=Grid}}">
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="auto" Background="Cyan"
>
<TextBlock Text="{Binding StrTest}" TextWrapping="NoWrap"/>
</ScrollViewer>
</Border>
and Convert
method can looks like:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is System.Windows.Controls.ColumnDefinition colDef)
{
return colDef.ActualWidth + colDef.Offset - int.Parse(parameter as string) ;
}
return value;
}
推荐阅读
- php - 重定向时会话未写入
- java - Spring boot和mySQL获取一对多关系的请求
- mysql - 如果是客户的第一个订单,计算订单日期
- html - 初学者:为什么HTML元素包括元素不调整高度
- c# - 将最大 400MB 大小的文件上传到 ASP.NET Core 3.1
- string - 如何将字符串转换为 rust 中的向量?
- python - 调用输入显示通道和用户参数
- c# - 我可以在 C# 中创建一个方法来尝试捕获一行代码吗?
- r - 在 R 中,使用 for 循环比较两个数据帧的字符串变量以创建指示两个数据帧中匹配的新标志变量?
- swift - SwiftUI:如何在列表中居中矩形?