0

我有一组我传递给Items的对象。我想直接将 every 传递给 each 。我怎样才能做到这一点?JSectionLongListMultiSelectorJSectioncontrols:Section

导致运行时异常的 XAML:

<toolkit:LongListMultiSelector ItemsSource="{Binding Items}">
    <toolkit:LongListMultiSelector.ItemTemplate>
        <DataTemplate>
            <controls:Section Data="{Binding}" />
        </DataTemplate>
    </toolkit:LongListMultiSelector.ItemTemplate>
</toolkit:LongListMultiSelector>

包含 XAML 的用户控件LongListMultiSelector

namespace Controls
{
    public partial class RemoteHomePage : UserControl
    {
        public ObservableCollection<JSection> Items { get; set; }

        public RemoteHomePage()
        {
            Items = new ObservableCollection<JSection> { };

            Items.Add(new JSection { id = 2, name = "Section 2" });
            Items.Add(new JSection { id = 1, name = "Section 1" });
            Items.Add(new JSection { id = 3, name = "Section 3" });

            InitializeComponent();
        }
    }
}

Section班级:

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
4

2 回答 2

0

您可以将任何集合绑定为 LongListSelector.ItemSource=Collection 任何 ListElement[i] 的数据上下文将自动设置为相应的 Collection[i] 元素。

于 2013-01-18T22:33:13.663 回答
0

经过一番搜索,我发现我根本不需要设置任何显式绑定,everyJSection将是DataContextevery的 a Section

此代码工作正常,不会导致异常:

<toolkit:LongListMultiSelector ItemsSource="{Binding Items}">
    <toolkit:LongListMultiSelector.ItemTemplate>
        <DataTemplate>
            <controls:Section />
        </DataTemplate>
    </toolkit:LongListMultiSelector.ItemTemplate>
</toolkit:LongListMultiSelector>
于 2013-01-18T19:50:50.620 回答