I was able to solve the problem by handling the Unfocused event in code-behind and then fire off a Command. I can provide a sample if anyone is interested.
InvalidOperationException using a Behavior in a ControlTemplate
I have developed (with help) a custom control using a ControlTemplate. An element of the control is an Entry where I want to trigger validation in my ViewModel when the control loses focus as this is the only way I can determine whether a required entry has been omitted.
Here is the relevant sample code:
<ContentView.ControlTemplate>
<ControlTemplate>
<Grid Margin="0"
RowDefinitions="*,*"
>
<StackLayout Margin="5"
Spacing="0"
>
<Entry Grid.Row="0"
FontSize="Medium"
HorizontalTextAlignment="{TemplateBinding HorizontalTextAlignment}"
Keyboard="{TemplateBinding Keyboard}"
Placeholder="{TemplateBinding Placeholder}"
PlaceholderColor="{TemplateBinding PlaceholderColor}"
ReturnType="{TemplateBinding ReturnType}"
Text="{TemplateBinding Text}"
TextColor="{TemplateBinding TextColor}"
>
<Entry.Behaviors>
<xct:EventToCommandBehavior Command="{TemplateBinding ValidateCommand}"
CommandParameter="{TemplateBinding ValidateCommandParameter}"
EventName="Unfocused"
/>
</Entry.Behaviors>
</Entry>
...
</ControlTemplate>
</ContentView.ControlTemplate>
I haven't yet transitioned my code from TemplateBinding; that is the subject of another question not relevant here, although the code-behind for the XAML does have all of the required BindingProperty's. If I run the sample in a ContentPage it works properly but if I run the Behavior in the ControlTemplate above I get "System.InvalidOperationException: 'Operation is not valid due to the current state of the object.'" The control works (without the desired behavior) without the Behavior.
Any suggestions would be very useful.