【发布时间】:2023-03-16 09:01:01
【问题描述】:
我对在 Windows Phone 7 Silverlight 中扩展 ListBox 的类有疑问。这个想法是有一个完整的ScrollViewer(黑色,例如填满整个手机屏幕)并且ItemsPresenter(红色)有一个边距(绿色)。这用于在整个列表周围留有边距,但滚动条从右上边缘开始,到深色矩形的右下边缘结束:
问题是,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):
Correct_01:顶部的滚动条
Correct_01:中间的滚动条
Correct_01:底部滚动条
错误示例:
Wrong_01:边距始终可见(例如:滚动位置中间)
唯一的解决方案是在列表末尾添加一个虚拟元素来补偿边距。我会尝试在控制逻辑中动态添加这个虚拟元素...在绑定的ObservableCollection 中添加一些逻辑,否则视图模型是没有选项的。
更新:我添加了我的最终解决方案作为单独的答案。 查看ExtendedListBox 课程。
【问题讨论】:
标签: silverlight windows-phone-7 xaml windows-phone-7.1