简体   繁体   中英

IsEnabled bound property not disabling entry field

I have used event to command behaviours on an entry field to trigger a command that disables another on screen entry field when the users starts typing in the current entry field. To be clear the event to command behaviour is working fine and so is the binding, meaning that I have put debug points on both and they are hit when the code is executed. The problem is that even though they are hit the IsEnabled property does not change on the other entry field.

The code:

The Xaml:

                  <Entry 
                   PlaceholderColor="Black"
                   TextColor="Black"                       
                   Text="{Binding Identity, Mode=OneWayToSource}"
                   Placeholder="Driver ID"
                   Grid.Column="0"
                   Grid.ColumnSpan="2"
                   Grid.Row="1">
                        <Entry.Behaviors>
                            <behaviors:FormEventToCommandBehavior EventName="TextChanged" Command="{Binding TextEntryTrigger}"/>
                        </Entry.Behaviors>
                    </Entry>

                    <Label Text="OR" TextColor="Black"
                   Grid.Column="0"
                   Grid.ColumnSpan="2"
                   Grid.Row="2"
                    VerticalOptions="Center"
                    />

                    <Entry 
                   PlaceholderColor="Black"
                   TextColor="Black"                       
                   Text="{Binding Identity, Mode=OneWayToSource}"
                   Placeholder="Route ID"
                   Grid.Column="0"
                   Grid.ColumnSpan="2"
                   Grid.Row="3"
                    IsEnabled="{Binding RouteIDVis}"/>

The ViewModel:

These command and properties are set in the right place I have just put them here for brevity.

public ICommand TextEntryTrigger { get; protected set; }

TextEntryTrigger = new Command(() => HandleEntryFieldVisibility());


public bool RouteIDVis
    {
        get { return _routeidvis; }
        set
        {
            _routeidvis = value;
            OnPropertyChanged(nameof(RouteIDVis));
        }
    }

    private void HandleEntryFieldVisibility()
    {
        RouteIDVis = false;
    }

I have left out the code for the event to command because as I stated when putting debug points in all of this they all get hit and I can see the RouteIDVis is being set to false but it does not disable the second entry field.

I have looked around but I cant seem to find a straight answer just some one sentence answers and no proper code answers.

Any ideas?

Have you tried using the solution from

https://forums.xamarin.com/discussion/71527/problem-with-binding-isenabled-property-of-an-entry

It make use of 2 Entry controls. The first Entry control is disabled while the second Entry control is enabled. The IsVisible ensures only one control is visible.

<ContentView Grid.Row="0"
                   Grid.Column="1"
                   IsVisible="{Binding EntryEnabled}">
        <Entry IsEnabled="True"
               Text="{Binding PropertyValue, Mode=TwoWay}"/>
</ContentView>

<ContentView Grid.Row="0"
             Grid.Column="1"
              IsVisible="{Binding EntryEnabled, Converter={StaticResource InverseConverter}}">
        <Entry IsEnabled="False"
               Text="{Binding PropertyValue, Mode=TwoWay}"/>
</ContentView>
<Entry Text="{Binding PropertyValue, Mode=TwoWay}"
       IsEnabled="False"/>

Change order of property, that may be a problem. It happened to me when using IsEnabled with button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM