问题是,ScrollViewer无法滚动到最后,它会截断列表中最后一个元素的50个像素。如果我使用StackPanel代替VirtualizingStackPanel,则边距是正确的,但是列表不再虚拟化。
感谢任何想法,我尝试了很多但没有效果。这是控件的错误吗?
解决方案:使用MyToolkit库中的ExtendedListBox控件的InnerMargin属性!
C#:
public class MyListBox : ListBox
{
public MyListBox()
{
DefaultStyleKey = typeof(MyListBox);
}
}
XAML(例如App.xaml):
<Application
x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="local:MyListBox">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ScrollViewer>
<ItemsPresenter Margin="30,50,30,50" x:Name="itemsPresenter" />
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="ListBoxItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Application.Resources>
...
</Application>
更新1:
我创建了一个简单的示例应用程序:滚动条无法在末尾滚动... 如果你在App.xaml中将VirtualizingStackPanel
更改为StackPanel
,它将按预期工作,但没有虚拟化。
更新2:
添加了一些示例图片。滚动条是蓝色的,以显示它们的位置。
预期结果(使用StackPanel
而不是VirtualizingStackPanel
):
正确_01:滚动条在顶部
正确_02:滚动条在中间
正确_03:滚动条在底部
错误示例:
错误_01:边距始终可见(例如:滚动位置中间)
唯一的解决方案是在列表末尾添加一个虚拟元素来补偿边距。我将尝试在控件逻辑中动态添加这个虚拟元素... 在绑定的ObservableCollection
或视图模型中添加一些逻辑不是选项。
更新: 我将我的最终解决方案作为单独的答案添加了进去。 请查看ExtendedListBox类。