I am getting a strange unhandled exception during databinding. I am databinding a ListBox with an IList. After populating the list with the objects, I set the DataContext of the ListBox to my object:
IList<Users> users = new List<Users>;
foreach(JToken jresult in Users)
{
User juser = JsonConvert.DeserializeObject<User>(jresult.ToString());
users.Add(juser);
this.DataContext = myObject;
My object has just two fields, name and email:
public class User
{
public string name { get; set; }
public string email { get; set; } }
On the XAML side, I am binding the ListItem and the text blocks within the ItemTemplate as follows:
<ListBox Name="Users" ItemsSource="{Binding}">
<TextBlock Text="{Binding Name}" Name="name" />
<TextBlock Text="{Binding Email}" Name="email" />
During debugging I can see the Users listbox is getting populated with correct items, however after exiting out of the method, the application crashes, and I am taken directly to the App.xaml.cs method Application_UnhandledException. The error details are:
e.ExceptionObject.InnerException
{"0x8000ffff"}
_data: null
_HResult: -2146233088
_innerException: null
_message: "0x8000ffff"
_methodDescs: {System.IntPtr[14]}
_optionalData: null
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146233088
InnerException: Could not evaluate expression
Message: "0x8000ffff"
StackTrace: " at MS.Internal.XcpImports.CheckHResult(UInt32 hr)\r\n at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)\r\n at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)\r\n at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)\r\n at MS.Internal.XcpImports.MeasureNative(IntPtr element, Single inWidth, Single inHeight)\r\n at MS.Internal.XcpImports.UIElement_Measure(UIElement element, Size availableSize)\r\n at System.Windows.UIElement.Measure(Size availableSize)\r\n at System.Windows.Controls.VirtualizingStackPanel.MeasureChild(UIElement child, Size layoutSlotSize)\r\n at System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)\r\n at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)\r\n at MS.Internal.XcpImpor
ts.MeasureOverrideNative(IntPtr element, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)\r\n at MS.Internal.XcpImports.FrameworkElement_MeasureOverride(FrameworkElement element, Size availableSize)\r\n at System.Windows.FrameworkElement.MeasureOverride(Size availableSize)\r\n at System.Windows.FrameworkElement.MeasureOverride(IntPtr nativeTarget, Single inWidth, Single inHeight, Single& outWidth, Single& outHeight)\r\n"
I have not been able to figure this out, and would really appreciate any guidance, or maybe a better approach for this kind of problem. One article did mention that the ListDictionaryInternal is no longer supported in SilverLight, so I am not sure if that could be the issue, as the error details point to the ListDictionaryInternal.