如何在Xamarin Forms中将ListView按钮的绑定上下文设置为父级的绑定上下文 [英] How to set Binding Context of ListView button to the binding context of parent in Xamarin Forms

查看:127
本文介绍了如何在Xamarin Forms中将ListView按钮的绑定上下文设置为父级的绑定上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个带有DataTemplate选择器的ListView,它使用基于ListView项的特定DataTemplate.

Basically, I have a ListView with a DataTemplate Selector that uses a particular DataTemplate basing on the ListView item.

现在,在DataTemplate中-我有一个带有命令的按钮,该命令应该绑定到Parent(或ListView)自身的ViewModel上.

Now, in the DataTemplate - I have a button w/ a command that should be binded on the ViewModel of the Parent (or the ListView) itself.

请注意,我只想绑定按钮的Command属性,因为文本和其他属性需要绑定到按钮的当前绑定上下文.

Note that I only want to bind the Command property of the button as the Text and other properties would need to be binded to the current binding Context of the button.

DataTemplate的BindingContext是ListView项目bindingContext(在这种情况下为消息模型),但我希望能够仅将数据模板中的特定按钮绑定到父listView的视图模型.

The BindingContext of the DataTemplate is the ListView Item bindingContext (Message Model in this case) but I want to be able to bind just a specific button in the data template to the viewmodel of the parent listView.

我该怎么做?

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage 
        xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        x:Class="MobileMobile.Views.MobileMessageListView"
        Title="Message List"
        NavigationPage.BarBackgroundColor="#FF9100"
        NavigationPage.BarTextColor="White"
        xmlns:ViewSwitchers="clr-namespace:MobileMobile.ViewSwitchers;assembly=MobileMobile"
        xmlns:ViewCells="clr-namespace:MobileMobile.ViewCells;assembly=MobileMobile"
        xmlns:Extensions="clr-namespace:MobileMobile.Extensions;assembly=MobileMobile"
        >

    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="botMessageDataTemplate">
                <ViewCell>
                    <Button Text="Hello!" Command="{Binding TestCommand, Source=???}" CommandParameter="Hello"/>
                </ViewCell>
            </DataTemplate>

            <ViewSwitchers:MobileMessageTemplateSwitcher x:Key="MobileMessageTemplateSwitcher" BotMessageDataTemplate="{StaticResource botMessageDataTemplate}" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <StackLayout Grid.Row="0" Orientation="Vertical" x:Name="MainViewStack">
                <ListView
                        CachingStrategy="RecycleElement"
                        BackgroundColor="Transparent"
                        ItemTemplate="{StaticResource MobileMessageTemplateSwitcher}"
                        IsPullToRefreshEnabled="true"
                        RefreshCommand="{Binding RefreshCommand}"
                        ItemsSource="{Binding Messages}"
                        HasUnevenRows="true"
                        IsRefreshing="{Binding IsLoading, Mode=OneWay}"
                        SeparatorVisibility="None">
                    <ListView.Footer/>
                </ListView>
            </StackLayout>
            <StackLayout Grid.Row="1" Orientation="Horizontal" HeightRequest="50">
                <Entry Text="{Binding CurrentMessageText}" Placeholder="{Binding MessageTextPlaceHolder}" HorizontalOptions="FillAndExpand"/>
                <Button Text="SEND" Command="{Binding SendMessageCommand}"/>
            </StackLayout>
        </Grid>
    </ContentPage.Content>
</ContentPage>

推荐答案

尝试一下:

<Button
    Text="Hello!"
    Command="{Binding Path=BindingContext. TestCommand, Source={x:Reference Name=MessageListPage}}"
    CommandParameter="Hello" />

您必须为Page提供一个值为MessageListPagex:Name属性,因此它与您的纯MVVM有点混乱,但是由于Xamarin XAML不支持相对绑定(据我所知.). ),这就是要走的路.

You would have to give the Page an x:Name property with the value MessageListPage, so it messes with your pure MVVM a bit, but since Xamarin XAML doesn't support relative binding (as far as I know..) this is the way to go.

这篇关于如何在Xamarin Forms中将ListView按钮的绑定上下文设置为父级的绑定上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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