簡體   English   中英

如何在 Xamarin Forms 中將 ListView 按鈕的綁定上下文設置為父級的綁定上下文

[英]How to set Binding Context of ListView button to the binding context of parent in Xamarin Forms

基本上,我有一個帶有 DataTemplate Selector 的 ListView,它使用基於 ListView 項的特定 DataTemplate。

現在,在 DataTemplate 中 - 我有一個帶有命令的按鈕,該按鈕應該綁定在父級(或 ListView)本身的 ViewModel 上。

請注意,我只想綁定按鈕的 Command 屬性,因為 Text 和其他屬性需要綁定到按鈕的當前綁定上下文。

DataTemplate 的 BindingContext 是 ListView Item bindingContext(在本例中為消息模型),但我希望能夠將數據模板中的特定按鈕綁定到父 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 一個x:Name屬性,其值為MessageListPage ,因此它與您的純 MVVM 有點混亂,但由於 Xamarin XAML 不支持相對綁定(據我所知......)這是方法去。

不是將數據模板聲明為內容字典中的靜態資源,而是將數據模板放在列表本身的項目模板中。

我不是 100% 確定幕后發生了什么,但是當聲明為靜態資源時,數據模板似乎與頁面的綁定上下文無關。 這意味着當您離開並返回頁面或更改列表視圖的內容時,您可能會在呈現項目更新時遇到問題。

這將使您能夠將按鈕命令綁定到父級,但如果您離開並重新訪問該頁面,也可以停止任何問題

<?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"
    x:Name="DeclareYourCodeBehindHereOrDeclareAViewModel"
    >

<ContentPage.Resources>
    <ResourceDictionary>
        <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"
                    IsPullToRefreshEnabled="true"
                    RefreshCommand="{Binding RefreshCommand}"
                    ItemsSource="{Binding Messages}"
                    HasUnevenRows="true"
                    IsRefreshing="{Binding IsLoading, Mode=OneWay}"
                    SeparatorVisibility="None">
                <ListView.ItemTemplate>
<DataTemplate x:Key="botMessageDataTemplate">
            <ViewCell>
                <Button Text="Hello!" Command="{Binding Source={x:Reference DeclareYourCodeBehindHereOrDeclareAViewModel}, Path=BindingContext.CommandName}" CommandParameter="Hello"/>
            </ViewCell>
        </DataTemplate>
                </ListView.ItemTemplate>
                <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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM