【问题标题】:WP7: Dynamically adding UIElements to a LongListSelector itemWP7:动态地将 UIElements 添加到 LongListSelector 项
【发布时间】: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


【解决方案1】:

您应该使用DataTemplateSelector 方法。

例如,使用字段 GridBorderStackPanel 定义枚举 ItemType,然后在 DataTemplateSelector 类中使用该枚举选择模板。

DataTemplateSelector请看以下文章:http://www.windowsphonegeek.com/articles/Implementing-Windows-Phone-7-DataTemplateSelector-and-CustomDataTemplateSelector

【讨论】:

  • 据我了解,这允许我为列表项选择不同的模板,但这不是问题 - 我可以只使用一个带有 Grid 的模板,这很好。问题是当向列表中添加许多项目时,它会因“参数不正确”而崩溃。异常,我不知道为什么。
  • 问题是您正在尝试绑定到 ContentProperty 网格对象,而 ContentPresenter 不知道如何显示该内容(网格)
  • 我怀疑这是问题所在,我不知道如何解决 - 我正在尝试其他涉及运行时可视化树的方法,但我还没有达到可接受的结果。跨度>
  • @Spiri 相信我,你需要改变方式。 DataTemplateSelector 和简单的枚举可以满足您的所有需求。
  • 我尝试了 DataTemplateSelector 方法(不知道我是否按照您的要求实现了它),但异常仍然存在。如果你想看看,我在这里添加了项目:ge.tt/7McuI6M/v/0?c
【解决方案2】:

ObservableCollection 用于ItemContent

【讨论】:

  • 您能否提供更多详细信息? ItemContent 应该是 ContentPresenter 内容(或 Border Child) - 我如何在此处集成 ObservableCollection?
猜你喜欢
  • 1970-01-01
  • 2013-05-06
  • 1970-01-01
  • 1970-01-01
  • 2012-09-10
  • 2015-07-05
  • 2021-08-22
  • 2017-01-01
  • 1970-01-01
相关资源
最近更新 更多