I'm using an Entry view inside of a ListView's ItemTemplate, and I want to bind a Command to the Completed event of the Entry view (ala MVVM). The top of the XAML with the DataTemplate definition looks like so:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:MedLemnMobile.Models"
xmlns:viewModel="clr-namespace:MedLemnMobile.ViewModels"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
x:Class="MedLemnMobile.Views.EquationLibPage"
Title="EquationSets"
Focused="ContentPage_Focused"
x:FieldModifier="public"
x:Name="myEqLibPage"
AutomationId="myEqLibPage"
BackgroundColor="{DynamicResource PageBackgroundColor}"
x:DataType="viewModel:EquationLibViewModel">
<ContentPage.BindingContext>
<viewModel:EquationLibViewModel/>
</ContentPage.BindingContext>
<ContentPage.Resources>
<DataTemplate x:Key="myEqSetDataTemplateViewCell_1">
<ViewCell>
<ViewCell.View Background="Goldenrod">
<StackLayout>
<Entry x:FieldModifier="public" x:Name="myEquationSetNameEntry"
x:DataType="models:EquationSet"
Text="{Binding EquationSetName, Mode=TwoWay}"
TextColor="{DynamicResource TextForegroundColor}">
<Entry.Behaviors>
<xct:EventToCommandBehavior
x:DataType="viewModel:EquationLibViewModel"
EventName="Completed"
Command="{Binding EquationSetNameCompletedCommand}"
CommandParameter="{x:Reference myEqLibPage}"/>
</Entry.Behaviors>
</Entry>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
Later in the XAML, the ListView definition looks like so:
<Grid Grid.Row="1" Grid.Column="0">
<ListView x:FieldModifier="public" x:Name="myEquationSetsListView"
AutomationId="myEquationSetsListView"
ItemsSource="{Binding EquationLib}"
SelectionMode="Single"
ItemTemplate="{StaticResource myEqSetDataTemplateViewCell_1}">
</ListView>
</Grid>
And, the Command declaration in the ViewModel looks like so:
namespace MedLemnMobile.ViewModels
{
public class EquationLibViewModel : ViewModelBase
{
public ICommand GotoMenuCommand { get; }
public ICommand GotoParamsCommand { get; }
public ICommand EquationSetNameCompletedCommand { get; }
public EquationLibViewModel()
{
GotoMenuCommand = new Command<EquationLibPage>(GotoMenu);
GotoParamsCommand = new Command<EquationLibPage>(GotoParams);
EquationSetNameCompletedCommand = new Command<EquationLibPage>(EquationSetNameCompleted);
}
I expect that when the user "Completes" entering the value in the Entry view, that the Command EquationSetNameCompletedCommand will fire. I have set a breakpoint at the beginning of the Command's code, and the breakpoint is never triggered. Any help figuring out why this Command is not firing will be greatly appreciated.
I got it to work! The answer was in the Command Binding as follows:
<Entry.Behaviors>
<xct:EventToCommandBehavior
x:DataType="viewModel:EquationLibViewModel"
EventName="Completed"
Command="{Binding Path=BindingContext.EquationSetNameCompletedCommand,
Source={Reference myEqLibPage}}"
CommandParameter="{Reference myEqLibPage}"/>
</Entry.Behaviors>