【问题标题】:Getting data to show in ListView DataTemplate获取要在 ListView DataTemplate 中显示的数据
【发布时间】:2016-01-05 02:12:58
【问题描述】:

这可能是我缺少的一些简单的东西,但我有一个带有 ItemTemplate 和 DataTemplate 的 ListView:

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

以及在后面的代码中传递给它的数据:

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

当我运行代码时,_ingredientViewModel.Ingredients 显示了从数据源传递给它的三个成分,但它们没有显示在 XAML 页面上。有什么想法吗?

【问题讨论】:

  • ListView上有databind方法吗...?

标签: c# xaml listview datatemplate


【解决方案1】:

如果您阅读了我的原始答案,请忽略它。 ItemsSource 没有在任何地方设置。设置 IngredientsListBox.ItemsSource,而不是设置 IngredientsListBox.DataContext

【讨论】:

  • 非常感谢!我知道我很笨。我很感激!
猜你喜欢
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
  • 2014-04-12
相关资源
最近更新 更多