Xamarin.Forms bindingContext将源设置回root/parent [英] Xamarin.Forms bindingContext Set the source back to root/parent

查看:78
本文介绍了Xamarin.Forms bindingContext将源设置回root/parent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个ViewModel,带有未调用的命令(AddToFavoriteCommand).现在,它仅照看CustomPin class中的命令,而不照看viewModel中的命令.我在后面的代码中将viewModel设置为页面的BindingContext.

I got a ViewModel with a command (AddToFavoriteCommand) that doesn't get called. Now it only looks after the command in the CustomPin class, not the viewModel. I'm setting my viewModel to the BindingContext of the page in the code behind.

但是因为它枚举了我的customPins集合,所以它会照看那里的命令.我需要回到根源.我可能需要更改源,但无法正常工作.

But since it enumerates my collection of customPins, it looks after the command there. I need to get back to the root. I probably need to change the source but can't get it to work.

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

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

Code behind (removed all other logic like clicked events that I didn't needed for this)

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

推荐答案

尝试如下:

<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元素添加到页面的根目录.当然,那里还有更多的属性,请将它们保留在那里,但是在这里我为清楚起见将它们省略了.

Notice how I added the x:Name element to the root of the page. Of course there are more attributes there, leave them there, but I left them out for clarity here.

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

Secondly, notice how I referred to that name from the MenuItem binding and added Path=BindingContext.. This way it will bind to the BindingContext of the element identified by the name, in our case the ContentPage.

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

A sample project can be found here: https://github.com/jfversluis/SampleParentBinding

这篇关于Xamarin.Forms bindingContext将源设置回root/parent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆