如何在 BindableLayout 中绑定到 MasterPage 视图模型 [英] How do I bind to MasterPage viewmodel inside a BindableLayout

查看:31
本文介绍了如何在 BindableLayout 中绑定到 MasterPage 视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的 Command 属性绑定到我的 MasterDetailPage 视图模型中的 Command.我尝试为母版页定义 name 属性并将其设置为参考源,但它似乎没有绑定到视图模型.对于上下文,List MenuItems 是同一 Viewmodel 中的一个属性,并且 Title 绑定得很好.我相信我没有正确设置命令的源,但不确定如何修复它.另外,我正在使用 Prism,因此视图模型已经映射到 app.xaml 中.

I am trying to bind my Command property to the Command in my MasterDetailPage view model. I tried defining the name property for the master page and setting that as the reference source, but it does not seem to bind to the viewmodel. For context, the List MenuItems is a property in the same Viewmodel and the Title binds just fine. I believe I am not setting the Source for the command correctly, but not sure how to fix it. Also, I am using Prism, so the viewmodel is mapped in app.xaml already.

<MasterDetailPage
x:Class="MasterDetailPrism.Views.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="http://prismlibrary.com"
prism:ViewModelLocator.AutowireViewModel="True"
x:Name="menuMasterPage">

<MasterDetailPage.Master>

    <ContentPage Title="Menu">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="225" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>


            <StackLayout Grid.Row="0" BackgroundColor="Aqua" />

            <StackLayout Grid.Row="1" BindableLayout.ItemsSource="{Binding MenuItems}">
                <BindableLayout.ItemTemplate>
                    <DataTemplate>
                        <StackLayout>
                            <Button
                                Command="{Binding NavigateAsync, Source={x:Reference menuMasterPage}}"
                                CommandParameter="{Binding NavPath}"
                                Text="{Binding Title}" />
                        </StackLayout>
                    </DataTemplate>
                </BindableLayout.ItemTemplate>
            </StackLayout>
        </Grid>

    </ContentPage>
</MasterDetailPage.Master>

推荐答案

我能够弄清楚 - 我必须命名 StackLayout,然后绑定到它的根作为源,而不是 contentpage 或 masterpage.在下面的代码中,ItemsList.

I was able to figure it out - I had to name the StackLayout, and then bind to the root of that as the source, not the contentpage or masterpage. In the code below, ItemsList.

        <StackLayout Grid.Row="1" BindableLayout.ItemsSource="{Binding MenuItems}" x:Name="ItemsList">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <Button
                            Command="{Binding BindingContext.NavigateAsync, Source={x:Reference ItemsList}}"
                            CommandParameter="{Binding NavPath}"
                            Text="{Binding Title}" />
                    </StackLayout>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>

这篇关于如何在 BindableLayout 中绑定到 MasterPage 视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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