C# 将绑定对象直接传递到DataTemplate项中
我有一个C# 将绑定对象直接传递到DataTemplate项中,c#,xaml,data-binding,windows-phone-8,C#,Xaml,Data Binding,Windows Phone 8,我有一个Items的jssection对象集合,我将其传递给LongListMultiSelector。我想直接将每个jssection传递到每个controls:Section。我该怎么做 导致运行时异常的XAML: <toolkit:LongListMultiSelector ItemsSource="{Binding Items}"> <toolkit:LongListMultiSelector.ItemTemplate> <DataT
Items
的jssection
对象集合,我将其传递给LongListMultiSelector
。我想直接将每个jssection
传递到每个controls:Section
。我该怎么做
导致运行时异常的XAML:
<toolkit:LongListMultiSelector ItemsSource="{Binding Items}">
<toolkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<controls:Section Data="{Binding}" />
</DataTemplate>
</toolkit:LongListMultiSelector.ItemTemplate>
</toolkit:LongListMultiSelector>
部分
类别:
namespace Controls
{
public partial class Section : UserControl
{
public JSection Data { get; set; }
public Section()
{
InitializeComponent();
}
}
}
我得到的例外情况:
An exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
An exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module. and wasn't handled before a managed/native boundary
An exception of type 'MS.Internal.NativeParseException' occurred in Unknown Module. and wasn't handled before a managed/native boundary
An exception of type 'System.Exception' occurred in Unknown Module. and wasn't handled before a managed/native boundary
经过一些搜索,我发现我根本不需要设置任何显式绑定,每个
jssection
都将是每个节的DataContext
此代码工作正常,不会导致任何异常:
<toolkit:LongListMultiSelector ItemsSource="{Binding Items}">
<toolkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<controls:Section />
</DataTemplate>
</toolkit:LongListMultiSelector.ItemTemplate>
</toolkit:LongListMultiSelector>
您可以将任何集合绑定为LongListSelector。ItemSource=collection
任何ListElement[i]的Datacontext将自动设置为相应的Collection[i]元素。有什么例外?Items属性的定义和设置在哪里?@JustinAngel我已经更新了我的问题。
<toolkit:LongListMultiSelector ItemsSource="{Binding Items}">
<toolkit:LongListMultiSelector.ItemTemplate>
<DataTemplate>
<controls:Section />
</DataTemplate>
</toolkit:LongListMultiSelector.ItemTemplate>
</toolkit:LongListMultiSelector>