简体   繁体   English

获取要在ListView DataTemplate中显示的数据

[英]Getting data to show in ListView DataTemplate

This is probably something simple I'm missing, but I have a ListView with an ItemTemplate and DataTemplate: 这可能是我想念的简单,但我有一个带有ItemTemplate和DataTemplate的ListView:

                         <ListView Name="IngredientsListBox">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding IngredientName}" />
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

and data passed to it in the code behind: 并在后面的代码中传递给它的数据:

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        var selectedRecipe = e.Parameter as Recipe;
        if (selectedRecipe != null)
        {

            _ingredientViewModel = new IngredientViewModel(selectedRecipe);
            IngredientsListBox.DataContext = _ingredientViewModel.Ingredients;
        }
    }

When I run the code, _ingredientViewModel.Ingredients shows the three ingredients passed to it from the data source, but they're not displaying on the XAML page. 当我运行代码时,_ingredientViewModel.Ingredients显示从数据源传递给它的三个成分,但它们没有显示在XAML页面上。 Any thoughts? 有什么想法吗?

Ignore my original answer if you read it. 如果您阅读它,请忽略我的原始答案。 The ItemsSource isn't being set anywhere. ItemsSource没有在任何地方设置。 Instead of setting IngredientsListBox.DataContext, set IngredientsListBox.ItemsSource 而不是设置IngredientsListBox.DataContext,设置IngredientsListBox.ItemsSource

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

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