【发布时间】:2012-08-09 17:35:49
【问题描述】:
我正在尝试以编程方式将 UIElements 添加到 LongListSelector 项目。我已经为页眉、页脚、项目等创建了模板。
现在我正在做这样的事情(列表项模板):
<DataTemplate x:Key="listItemTemplate">
<ContentPresenter Content="{Binding ItemContent}"></ContentPresenter>
</DataTemplate>
在模型内部,我将 ItemContent 分配给一个网格,在该网格上添加不同的 UI 元素。我真的不知道这是否是这样做的方法,或者这是否可能在 LongListSelector 上但滚动列表时我得到一个“参数不正确”。异常(起初,一切似乎都很好,添加的所有元素看起来都很好)。我尝试了不同的方法,但都没有奏效,我觉得我只是在没有特定想法的情况下随机尝试解决方案。有没有人设法通过使用 LongListSelector 做到这一点(任何建议将不胜感激)?
调用栈:
LongListSelectorPOC.dll!LongListSelectorPOC.App.Application_UnhandledException(object sender, System.Windows.ApplicationUnhandledExceptionEventArgs e) Line 102 + 0x5 bytes C#
System.Windows.dll!MS.Internal.Error.CallApplicationUEHandler(System.Exception e) + 0x30 bytes
System.Windows.dll!MS.Internal.Error.GetXresultForUserException(System.Exception ex) + 0x4d bytes
System.Windows.dll!MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdate(System.IntPtr oldParentElement, System.IntPtr parentElement, System.IntPtr childElement, byte bIsParentAlive, byte bKeepReferenceToParent, byte bCanCreateParent) + 0x6b bytes
[External Code]
System.Windows.dll!MS.Internal.XcpImports.MeasureOverrideNative(System.IntPtr element, float inWidth, float inHeight, out float outWidth, out float outHeight)
System.Windows.dll!MS.Internal.XcpImports.FrameworkElement_MeasureOverride(System.Windows.FrameworkElement element, System.Windows.Size availableSize) + 0x26 bytes
System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.Windows.Size availableSize) + 0x7 bytes
System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget, double inWidth, double inHeight, out double outWidth, out double outHeight) + 0x43 bytes
[External Code]
System.Windows.dll!MS.Internal.XcpImports.Measure_WithDesiredSizeNative(System.IntPtr element, float inWidth, float inHeight, out float outWidth, out float outHeight)
System.Windows.dll!MS.Internal.XcpImports.UIElement_Measure_WithDesiredSize(System.Windows.UIElement element, System.Windows.Size availableSize) + 0x26 bytes
System.Windows.dll!System.Windows.UIElement.Measure_WithDesiredSize(System.Windows.Size availableSize) + 0x39 bytes
System.Windows.dll!System.Windows.Controls.VirtualizingStackPanel.MeasureChild(System.Windows.UIElement child, System.Windows.Size layoutSlotSize) + 0x42 bytes
System.Windows.dll!System.Windows.Controls.VirtualizingStackPanel.GeneratePreviousChild(int childIndex, System.Windows.Size layoutSlotSize) + 0x6f bytes
System.Windows.dll!System.Windows.Controls.VirtualizingStackPanel.GeneratePreviousItems(ref double logicalVisibleSpace, ref System.Windows.Size stackDesiredSize, System.Windows.Size layoutSlotSize, bool isHorizontal, bool adjustPositions) + 0x138 bytes
System.Windows.dll!System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(System.Windows.Size constraint) + 0x4e1 bytes
System.Windows.dll!System.Windows.FrameworkElement.MeasureOverride(System.IntPtr nativeTarget, double inWidth, double inHeight, out double outWidth, out double outHeight) + 0x43 bytes
[External Code]
【问题讨论】:
-
ItemContent 里面是什么?它是什么类型的? @Spiri
-
我尝试使用 Grid、StackPanel 甚至使用带有 Grid 子项的 Border - 当我在 LongListSelector 中添加许多项目时仍然出现异常
-
没有@Spiri。您正在将 ContentPresenter 的内容绑定到 ItemContent。这个属性(ItemContent)中保存了什么?
-
您能分享一下您是如何使用该数据模板 listItemTemplate 的吗?
-
我这样分配它:'listSelector.ItemTemplate = Application.Current.Resources["listItemTemplate"] as DataTemplate;'现在在模型内部它是一个网格:'public Grid ItemContent { get { return content; } 设置 { 内容 = 价值; OnPropertyChanged("ItemContent"); } }'
标签: silverlight windows-phone-7 data-binding datatemplate longlistselector