[英].NET MAUI 7 - ListView MenuItem CommandParameter not binded correctly
我的問題和這個類似 菜單項 + 命令參數 = 空? 為什么?
我的MenuItem
中嵌套在ViewCell.ContextActions
中的CommandParameter="{Binding.}"
在我閱讀時始終為 null。 我也試過CommandParameter="{Binding}"
。
這是完整的代碼:
看法
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodel="clr-namespace:CookUs.ViewModel"
xmlns:model="clr-namespace:CookUs.Model"
x:Class="CookUs.View.ViewRecipePage"
x:DataType="viewmodel:ViewRecipeViewModel"
Title="{Binding Recipe.Name}">
<ScrollView>
<VerticalStackLayout>
<ListView ItemsSource="{Binding Recipe.Ingredients}"
x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Add to Cart" IconImageSource="add_to_cart.png"
Command="{Binding Source={x:Reference listView}, Path=BindingContext.AddToCartCommand}"
CommandParameter="{Binding .}" />
</ViewCell.ContextActions>
<StackLayout x:DataType="model:Ingredient">
<Label Text="{Binding Name}" FontAttributes="Bold" FontSize="Small" HorizontalTextAlignment="Center"/>
<Label Text="{Binding Quantity}" FontSize="Micro" HorizontalTextAlignment="Center"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</VerticalStackLayout>
</ScrollView>
視圖模型
public Command AddToCartCommand { get; }
public ViewRecipeViewModel()
{
AddToCartCommand = new Command(OnAddToCartAsync);
}
private async void OnAddToCartAsync(object obj)
{
if (obj == null) return;
Ingredient i = obj as Ingredient;
if(!(await DataStore.AddToCartAsync(i))) {
await Application.Current.MainPage.DisplayAlert("Error", "Failed to add to cart", "OK");
}
OnPropertyChanged(nameof(Cart));
}
模型
namespace CookUs.Model
{
public class Ingredient
{
public string Name { get; set; }
public string Quantity { get; set; }
}
}
您的問題是x:DataType
。 使用 Compiled Bindings 時,您需要在 BindingContext 發生變化的任何地方設置正確的 DataType。 在這種情況下,您需要在DataTemplate
而不是StackLayout
上添加x:DataType="Ingredient"
:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewmodel="clr-namespace:CookUs.ViewModel"
xmlns:model="clr-namespace:CookUs.Model"
x:Class="CookUs.View.ViewRecipePage"
x:DataType="viewmodel:ViewRecipeViewModel"
Title="{Binding Recipe.Name}">
<ScrollView>
<VerticalStackLayout>
<ListView ItemsSource="{Binding Recipe.Ingredients}"
x:Name="listView">
<ListView.ItemTemplate>
<DataTemplate
x:DataType="model:Ingredient">
<ViewCell>
<ViewCell.ContextActions>
<MenuItem Text="Add to Cart" IconImageSource="add_to_cart.png"
Command="{Binding Source={x:Reference listView}, Path=BindingContext.AddToCartCommand}"
CommandParameter="{Binding .}" />
</ViewCell.ContextActions>
<StackLayout>
<Label Text="{Binding Name}" FontAttributes="Bold" FontSize="Small" HorizontalTextAlignment="Center"/>
<Label Text="{Binding Quantity}" FontSize="Micro" HorizontalTextAlignment="Center"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</VerticalStackLayout>
</ScrollView>
注意.
這是綁定的一部分:
CommandParameter="{Binding.}"
它意味着將整個項目作為參數傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.