如何在 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.
Basically, I have a ListView with a DataTemplate Selector that uses a particular DataTemplate basing on the ListView item.
现在,在 DataTemplate 中 - 我有一个带有命令的按钮,该按钮应该绑定在父级(或 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 属性,因为 Text 和其他属性需要绑定到按钮的当前绑定上下文.
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 Item 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" />
您必须为页面提供一个 x:Name
属性,其值为 MessageListPage
,因此它与您的纯 MVVM 有点混乱,但由于 Xamarin XAML 没有t 支持相对绑定(据我所知..)这是要走的路.
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屋!