Rav*_*ven 5 c# xaml mvvm xamarin xamarin.forms
基本上,我有一个带有DataTemplate选择器的ListView,它使用基于ListView项的特定DataTemplate.
现在,在DataTemplate中 - 我有一个带有命令的按钮,应该在Parent(或ListView)本身的ViewModel上绑定.
请注意,我只想绑定按钮的Command属性,因为Text和其他属性需要绑定到按钮的当前绑定Context.
DataTemplate的BindingContext是ListView Item bindingContext(在本例中为Message Model),但我希望能够将数据模板中的特定按钮绑定到父listView的viewmodel.
我怎么做?
<?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>
Run Code Online (Sandbox Code Playgroud)
Ger*_*uis 21
试试这个:
<Button
Text="Hello!"
Command="{Binding Path=BindingContext. TestCommand, Source={x:Reference Name=MessageListPage}}"
CommandParameter="Hello" />
Run Code Online (Sandbox Code Playgroud)
你将不得不给页面的x:Name与值属性MessageListPage,因此它可以与你的纯MVVM弄乱了一点,但因为Xamarin XAML不支持相对结合(据我所知..),这是要走的路.
| 归档时间: |
|
| 查看次数: |
10302 次 |
| 最近记录: |