0

我試圖以編程方式將UIElement s添加到LongListSelector項目。我創建模板頁眉,頁腳,項目等WP7:將UIElements動態添加到LongListSelector項目

現在我做這樣的事情(列表項模板):

<DataTemplate x:Key="listItemTemplate"> 
    <ContentPresenter Content="{Binding ItemContent}"></ContentPresenter> 
</DataTemplate> 

模型裏面,我assisgn的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]

+0

ItemContent內部是什麼?它是什麼類型? @Spiri – 2012-08-13 17:12:52

+0

我嘗試了一個Grid,一個StackPanel,甚至帶有一個帶有Grid子元素的邊框 - 當我在LongListSelector內添加了很多項時仍然有異常 – Spiri 2012-08-13 20:48:39

+0

否@Spiri。您將ContentPresenter的內容綁定到ItemContent。該屬性(ItemContent)中保存了什麼? – 2012-08-14 07:03:00

回答

0

您應該使用DataTemplateSelector方法。

定義,例如,與場GridBorderStackPanel和枚舉的ItemType然後DataTemplateSelector類選擇模板使用該枚舉。

對於DataTemplateSelector,請看看下面的文章:http://www.windowsphonegeek.com/articles/Implementing-Windows-Phone-7-DataTemplateSelector-and-CustomDataTemplateSelector

+0

據我所知,這可以讓我爲列表項目選擇不同的模板,但這不是問題 - 我只能使用一個模板和網格一起工作,那很好。問題是,當向列表添加很多項時,它會崩潰,「參數不正確」。例外,我不知道爲什麼。 – Spiri 2012-08-14 08:24:12

+0

問題是您正在嘗試綁定到ContentProperty Grid對象並且ContentPresenter不知道如何顯示該內容(Grid) – 2012-08-15 07:02:54

+0

我懷疑這是問題,我不知道如何解決它 - 我正在嘗試其他方法涉及運行時可視化樹,但我還沒有達到可接受的結果。 – Spiri 2012-08-15 09:29:46

0

使用ObservableCollectionItemContent

+0

你能否提供更多的細節? ItemContent應該是ContentPresenter內容(或邊界子) - 我如何在這裏集成一個ObservableCollection? – Spiri 2012-08-14 10:53:02