简体   繁体   中英

Data Binding for nested controls

I want to do search inside some books, then result of each book appears in separate item of pivot control. each one is represented by a separate LongListSelector control inside that PivotItem. Now I want to know should I assing ItemsSource for LongListSelector or for its parent which is a Pivot?

There is a dictionary for all books:

private Dictionary<string, List<Model>> ItemResources = new Dictionary<string, List<Model>>();

and a List<Model> for each book, which will be saved as a value inside ItemResources above.

this is what I do:

        foreach (var translation in ItemResources)
        {
            PivotItem pivotItem = new PivotItem
            {
                Header = translation.Key
            };

            LongListSelector lls = new LongListSelector
            {
                HideEmptyGroups = false,
                IsGroupingEnabled = false
            };

            lls.ItemTemplate = Resources["template"] as DataTemplate;
            lls.ItemsSource = translation.Value;

            pivotItem.Content = lls;

            ResultPivot.Items.Add(pivotItem);
        }

and the template is a reusable DataTemplate which I redproduce it for each longlistselector inside each pivotItem of the ResultPivot :

<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>

The problem is that nothing appears on the screen after running. I saw in debugging, that the values are there, but it seems something is wrong with data binding. How can I solve it? thanks

(this is a Windows Phone 8 App, but because the concept is the same for WPF and its wide community I added it too)

The UI has currently no way of knowing when the source changes. You need to either use an ObservableCollection or implement the INotifyPropertyChanged interface to properly notify the UI when the source changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM