简体   繁体   English

嵌套控件的数据绑定

[英]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. 每个项目都由该PivotItem内部的单独的LongListSelector控件表示。 Now I want to know should I assing ItemsSource for LongListSelector or for its parent which is a Pivot? 现在,我想知道应该为LongListSelector还是为其父项(枢轴)配置ItemsSource

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. 以及每本书的List<Model> ,将其另存为上述ItemResources的值。

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 : template是一个可重复使用的DataTemplate我redproduce它为每个pivotItem内的每个longlistselector 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) (这是Windows Phone 8应用程序,但是由于WPF及其广泛社区的概念相同,我也添加了它)

The UI has currently no way of knowing when the source changes. UI当前无法知道源何时更改。 You need to either use an ObservableCollection or implement the INotifyPropertyChanged interface to properly notify the UI when the source changes. 您需要使用ObservableCollection或实现INotifyPropertyChanged接口来在源更改时正确通知UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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