当单个项目大于列表框大小时,如何让列表框滚动条工作。
我有一个带有自定义 ItemTemplate 的列表框。该模板显示用于记笔记的消息和时间戳。问题是,如果运算符(operator)创建了一个非常长的注释,则 ListBox 滚动条不会出现,并且不允许您上下滚动以查看整个注释。该控件非常适合几个小音符。如果您在大音符之后添加一个小音符,则会出现滚动条,您可以向下滚动,但您看不到整个第一个音符,只能看到之前可见的部分,然后它会在您滚动时捕捉到下一个音符。
<ListBox Grid.Row="1" ItemsSource="{Binding Notes}" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Margin="1" Padding="5" BorderThickness="1" BorderBrush="SteelBlue" CornerRadius="3">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Text="{Binding CreatedDateTime, StringFormat={}{0:h:mm:ss tt}}" Margin="0,0,20,0"/>
<TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Note}" TextWrapping="Wrap" FontSize="20"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
帮助解释的图片。
最佳答案
我尝试了 ScrollViewer,但它最终导致了额外的问题。我必须开始路由滚动事件而不是触摸事件等。
最后我找到了一个简单的属性来设置。 在 ListBox 上设置 'ScrollViewer.CanContentScroll="False"'。
这改变了滚动行为,内容不再被截断。所有事件也有效。
我真的不明白为什么会这样,但我认为这与 ListBox 以及它如何通过不在屏幕上呈现项目来尝试优化性能有关。我认为这会禁用性能增强功能,但可以使滚动正常工作,因为它现在为每个项目呈现了空间。
关于c# - 如果单个项目超出控件高度,WPF 列表框无法显示内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41571564/