Xaml 工具箱:列表框中的ExpanderView

Xaml 工具箱:列表框中的ExpanderView,xaml,data-binding,windows-phone-8,silverlight-toolkit,Xaml,Data Binding,Windows Phone 8,Silverlight Toolkit,我无法将数据绑定到列表框以动态创建ExpanderView 我以前在代码中使用过列表框,所以我不确定绑定是否真的是问题所在。我是否将ExpanderView的内容设置错误 到目前为止我在XAML中的代码: <ListBox x:Name="Newsticker_Listbox"> <ListBox.ItemTemplate> <DataTempla

我无法将数据绑定到列表框以动态创建ExpanderView

我以前在代码中使用过列表框,所以我不确定绑定是否真的是问题所在。我是否将ExpanderView的内容设置错误

到目前为止我在XAML中的代码:

                <ListBox x:Name="Newsticker_Listbox">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <toolkit:ExpanderView Header="{Binding}" Expander="{Binding}" ItemsSource="{Binding}"
                              HeaderTemplate="{StaticResource CustomHeaderTemplate}"
                             ExpanderTemplate="{StaticResource CustomExpanderTemplate}"
                              ItemTemplate="{StaticResource CustomItemTemplate}" />
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
模板:

<!--newsfeed templates-->
<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="CustomHeaderTemplate">
        <TextBlock Text="{Binding Title}" />
    </DataTemplate>
    <DataTemplate x:Key="CustomExpanderTemplate">
        <Image Source="{Binding Subtitle}" />
    </DataTemplate>
    <DataTemplate x:Key="CustomItemTemplate">
        <TextBlock Text="{Binding Title}" />
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>
我和你有关系

ObservableCollection<news> newslist = new ObservableCollection<news>();
//populate
Newsticker_Listbox.ItemsSource = newslist; 
新闻是一个非常简单的对象,它只存储一些字符串,可以由news.Title、news.Subtitle等读取

我使用了来自的示例作为我的代码的基础,我只是为了我的原因对其进行了简化,可能太多了

非常感谢您的帮助,提前谢谢大家

编辑

到目前为止,这段代码仍然有效,但是为什么使用模板的解决方案不起作用呢

<ListBox x:Name="Newsticker_Listbox">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto"/>
                                    <RowDefinition Height="10"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                </Grid.ColumnDefinitions>
                                <toolkit:ExpanderView>
                                    <toolkit:ExpanderView.Header>
                                        <TextBlock Text="{Binding Titel}" />
                                    </toolkit:ExpanderView.Header>
                                    <toolkit:ExpanderView.Expander>
                                        <TextBlock Text="{Binding Untertitel}" />
                                    </toolkit:ExpanderView.Expander>
                                    <toolkit:ExpanderView.Items>
                                        <TextBlock Text="{Binding Text}" />
                                    </toolkit:ExpanderView.Items>
                                </toolkit:ExpanderView>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
删除头={Binding}和扩展器={Binding}XAML代码。它将覆盖您的自定义HeaderTemplate和ExpanderTemplate


您将标头和扩展器绑定到应用程序的上下文,而应用程序并不真正理解该上下文,因此绑定本身没有解析为任何内容,因此您的ExpanderView没有显示任何内容。

什么类型的是newlist?如果它是List或ObservableCollection或SomeClass的任何其他集合,请在问题中发布您的SomeClass定义是的,您是对的,我忘记了定义。我已经编辑了这篇文章。请尝试从XAML中的列表框定义中删除此部分:ItemsSource={Binding Collection}。因为您从代码中分配了ListBox的ItemsSource,所以我认为您不需要在XAML中绑定ItemsSource扩展器仍然不会显示,但我已经在其他ListBox中尝试了这一点,并且实际上,使用ItemsSource={Binding Collection}与不使用ItemsSource={Binding Collection}没有区别。也许这是解决方案的一部分,谢谢!我发现在使用混合样本数据时添加了ItemsSource={Binding Collection}。不幸的是,这并没有解决我的问题。另外,当我删除ItemsSource={Binding}时,仍然没有显示任何项目。