Xamarin.Forms绑定上下文:将源设置回根/父级

7
我有一个带有命令(AddToFavoriteCommand)的ViewModel,但该命令未被调用。现在它只在CustomPin类中查找命令,而不是viewModel。我在代码后台将我的viewModel设置为页面的BindingContext。但由于它枚举了我的customPins集合,因此在那里查找命令。我需要回到根源。我可能需要更改源,但无法使其工作。
<ContentPage.Content>
    <StackLayout>
        <AbsoluteLayout>
            <Button Text="{Binding Filter}" Command="{Binding GotoFilterPageCommand}" />
        </AbsoluteLayout>
        <ListView x:Name="ListView" RowHeight="60" ItemsSource="{Binding CustomPins}" ItemSelected="OnItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                            <MenuItem Text="Favorite" Command="{Binding AddToFavoriteCommand}" />
                            <MenuItem Text="..." CommandParameter="{Binding .}" Clicked="OnMoreClicked" />
                        </ViewCell.ContextActions>
                        <StackLayout Padding="5" Orientation="Vertical" >
                            <Label Text="{Binding ParkingLot.Street}" FontSize="Medium" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

代码后端(删除了所有其他逻辑,如我不需要的点击事件)

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ParkingListPage : ContentPage
{
    public ParkingListPage()
    {
        InitializeComponent();
        BindingContext = new ParkingListViewModel();
    }
}

我的ViewModel

public class ParkingListViewModel
{
    public ParkingListViewModel()
    {
        AddToFavoriteCommand = new Command(Test);
    }

    private void Test()
    {
    }

    public IEnumerable<CustomPin> CustomPins { get; set; } = SampleParkings.Parkings;

    public Command AddToFavoriteCommand { get; }
}
1个回答

18

像这样尝试:

<ContentPage x:Name="YourPageName">
    <ContentPage.Content>
        <StackLayout>
            <AbsoluteLayout>
                <Button Text="{Binding Filter}" Command="{Binding GotoFilterPageCommand}" />
            </AbsoluteLayout>
            <ListView x:Name="ListView" RowHeight="60" ItemsSource="{Binding CustomPins}" ItemSelected="OnItemSelected">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <ViewCell.ContextActions>
                                <MenuItem Text="Favorite" Command="{Binding Path=BindingContext.AddToFavoriteCommand, Source={x:Reference YourPageName}}" />
                                <MenuItem Text="..." CommandParameter="{Binding .}" Clicked="OnMoreClicked" />
                            </ViewCell.ContextActions>
                            <StackLayout Padding="5" Orientation="Vertical" >
                                <Label Text="{Binding ParkingLot.Street}" FontSize="Medium" />
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

请注意,我在页面的根元素中添加了x:Name元素。当然,还有更多属性,保留它们,但在这里为了清晰起见,我将它们省略了。

其次,请注意我是如何从MenuItem绑定引用该名称并添加Path=BindingContext.的。这样它将绑定到由名称标识的元素的BindingContext,在我们的情况下是ContentPage

可以在此处找到示例项目:https://github.com/jfversluis/SampleParentBinding


这应该与视图模型一起工作。请更新您的问题,附上设置视图模型的代码。 - Gerald Versluis
我更新了我的代码后端。我删除了所有其他方法,如点击事件。如果有必要,我也添加了我的viewModel。 - thatsIT
很奇怪,因为它不起作用。它仍然调用我的代码后台。我已经将其从代码后台中删除以避免一些冲突,但仍然是相同的问题。这可能与我的contentpage标签有关吗?xmlns="http://xamarin.com/schemas/2014/forms",xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" - thatsIT
如果明天你还没有其他项目的话,我会为你准备一个样例项目。 - Gerald Versluis
1
我在 MenuItem 的绑定中忘记了一个非常重要的小细节,现在我已经更新了答案并添加了一个示例项目。谢谢! - Gerald Versluis
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接