C# 嵌套控件的数据绑定

C# 嵌套控件的数据绑定,c#,wpf,windows-phone-7,data-binding,windows-phone-8,C#,Wpf,Windows Phone 7,Data Binding,Windows Phone 8,我想在一些书中进行搜索,然后每本书的结果显示在pivot控件的单独项中。每个数据透视项都由该数据透视项中单独的LongListSelector控件表示。现在我想知道我应该为LongListSelector分配ItemsSource,还是为它的父项分配一个Pivot 所有书籍都有一本字典: private Dictionary<string, List<Model>> ItemResources = new Dictionary<string, List<Mod

我想在一些书中进行搜索,然后每本书的结果显示在pivot控件的单独项中。每个数据透视项都由该数据透视项中单独的LongListSelector控件表示。现在我想知道我应该为LongListSelector分配
ItemsSource
,还是为它的父项分配一个Pivot

所有书籍都有一本字典:

private Dictionary<string, List<Model>> ItemResources = new Dictionary<string, List<Model>>();
模板
是一个可重用的数据模板,我为
ResultPivot的每个数据透视项内的每个longlistselector重新生成它:

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="template">
        <StackPanel Margin="0,0,0,0" Orientation="Vertical">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="Auto" VerticalAlignment="Top" Width="455" Margin="3,20,3,0">
                <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" FontSize="{StaticResource PhoneFontSizeNormal}"/>
                <TextBlock Text="{Binding Number}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" FontSize="{StaticResource PhoneFontSizeNormal}"/>
            </StackPanel>
            <StackPanel Height="Auto" VerticalAlignment="Top" HorizontalAlignment="Left" Width="455" Margin="3,0,3,20">
                <TextBlock Text="{Binding Text}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" FontSize="{StaticResource PhoneFontSizeNormal}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

问题在于运行后屏幕上没有显示任何内容。我在调试中看到,这些值存在,但数据绑定似乎有问题。我怎样才能解决它?谢谢


(这是一个Windows Phone 8应用程序,但由于WPF及其广泛社区的概念相同,我也添加了它)

用户界面目前无法知道源代码何时更改。您需要使用
ObservableCollection
或实现
INotifyPropertyChanged
接口,以便在源代码更改时正确通知UI。

否,请告诉我们此代码有什么问题!你到底有什么问题?什么不起作用?@Haspemulator对不起,我正要换那个部分,你说得对。已编辑。在调试输出窗口中没有看到任何数据绑定错误或警告吗?正在设置模板/控件的DataContext在哪里?您是否正确地实现了INotifyPropertyChanged
?噢,嗯,噢。请给我一块砖。。没有ObservableCollection或NotifyPropertyChanged!!,我刚刚在代码隐藏中添加了一个私有字段,并将其用于
DataContext
。但我不知道刚才这是怎么回事?
<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="template">
        <StackPanel Margin="0,0,0,0" Orientation="Vertical">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Height="Auto" VerticalAlignment="Top" Width="455" Margin="3,20,3,0">
                <TextBlock Text="{Binding Name}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" FontSize="{StaticResource PhoneFontSizeNormal}"/>
                <TextBlock Text="{Binding Number}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" FontSize="{StaticResource PhoneFontSizeNormal}"/>
            </StackPanel>
            <StackPanel Height="Auto" VerticalAlignment="Top" HorizontalAlignment="Left" Width="455" Margin="3,0,3,20">
                <TextBlock Text="{Binding Text}" TextWrapping="Wrap" Style="{StaticResource PhoneTextLargeStyle}" FontSize="{StaticResource PhoneFontSizeNormal}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>