C# 如何在ContextActions Xamarin MVVM中设置项

C# 如何在ContextActions Xamarin MVVM中设置项,c#,xamarin,mvvm,C#,Xamarin,Mvvm,如何在ContextActions中设置项。在我的示例中,我想设置默认汽车 <ListView.ItemTemplate> <DataTemplate> <ViewCell> <ViewCell.ContextActions> <MenuItem Text="Usta

如何在ContextActions中设置项。在我的示例中,我想设置默认汽车

<ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                             <MenuItem Text="Ustaw domyslny" Command="{Binding DefultCarCommand}" CommandParameter="{Binding .}"/>
                        </ViewCell.ContextActions>
                        <StackLayout>
                            <Label Text="{Binding RegistrationNumber}" />
                            <Label Text="{Binding Brand}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>

现在它不起作用了,我也不知道为什么

请注意,菜单项的BindingContext是您在ListView的ItemSource中绑定的单个模型,而不是整个ViewModel。您需要使用引用绑定绑定到ViewModel属性,如下所示:

<ListView x:Name="MyListView">
    <ListView.ItemTemplate>
        <DataTemplate>
        <ViewCell>
            <ViewCell.ContextActions>
                <MenuItem Text="Ustaw domyslny" Command="{Binding Path=BindingContext.DefultCarCommand, Source={x:Reference Name=MyListView}}" CommandParameter="{Binding .}"/>
            </ViewCell.ContextActions>
            <StackLayout>
                <Label Text="{Binding RegistrationNumber}" />
                <Label Text="{Binding Brand}" />
            </StackLayout>
        </ViewCell>
        </DataTemplate>
     </ListView.ItemTemplate>
</ListView>


什么不起作用?你的命令没有被触发吗?你的应用程序崩溃了吗?还有别的吗?绑定不起作用。我不能运行defultcar命令
<ListView x:Name="MyListView">
    <ListView.ItemTemplate>
        <DataTemplate>
        <ViewCell>
            <ViewCell.ContextActions>
                <MenuItem Text="Ustaw domyslny" Command="{Binding Path=BindingContext.DefultCarCommand, Source={x:Reference Name=MyListView}}" CommandParameter="{Binding .}"/>
            </ViewCell.ContextActions>
            <StackLayout>
                <Label Text="{Binding RegistrationNumber}" />
                <Label Text="{Binding Brand}" />
            </StackLayout>
        </ViewCell>
        </DataTemplate>
     </ListView.ItemTemplate>
</ListView>