InvalidOperationException using a Behavior in a ControlTemplate

Gary Lewis 256 Reputation points
2020-12-12T21:55:21.307+00:00

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.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,354 questions
0 comments No comments
{count} votes

Accepted answer
  1. Gary Lewis 256 Reputation points
    2020-12-14T08:33:32.693+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.