获取数据以在 ListView DataTemplate 中显示
Getting data to show in ListView DataTemplate
这可能是我遗漏的简单内容,但我有一个带有 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 页面上。有什么想法吗?
看到原答案请忽略。 ItemsSource 未在任何地方设置。不要设置 IngredientsListBox.DataContext,而是设置 IngredientsListBox.ItemsSource
这可能是我遗漏的简单内容,但我有一个带有 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 页面上。有什么想法吗?
看到原答案请忽略。 ItemsSource 未在任何地方设置。不要设置 IngredientsListBox.DataContext,而是设置 IngredientsListBox.ItemsSource