0
votes

I have created a ViewModel related to a custom Xamarin Forms Map

Over this Map, I am displaying an Entry field, on a trigger to be visible or invisible according to conditions

I have successfully bound the Entry IsVisible parameter to the behavior, and can toggle it on a button

However, what I actually want is to achieve this in response to the click on a Pin

When I attempt this, i cannot get the OnPropertyChange to fire

I'm not sure how to overcome this - the pin is in a separate class so I cannot bind it to the Grid view that contains the text entry

I've attached a click event to the custom pin and used it to interact with the view model

        pin.Clicked += (object sender, EventArgs e) =>
        {
            var p = sender as CustomPin;

            var callingMethod = new HandleEditor();

            callingMethod.Pin_Clicked(p);

        };

This is the XAML

    <Grid x:Name="LayoutGrid">

    <Grid.BindingContext>
        <local:HandleEditor />
    </Grid.BindingContext>

<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="150" />
    <RowDefinition Height="*" />     
</Grid.RowDefinitions>

    <local:CustomMap Grid.RowSpan="3"  Grid.Row="0"  MapType="Street" WidthRequest="300" HeightRequest="300" />                        

    <Editor x:Name="TextEntry" Grid.Row="1" VerticalOptions="FillAndExpand" IsVisible="{Binding SwitchVisible, Mode=TwoWay}"/>

    <Button Grid.Row="2" Text="Switch Bool Test" Command="{Binding ChangeBoolValue}"/>

    </Grid>

This is the ViewModel

    public event PropertyChangedEventHandler PropertyChanged;

    bool isEditorVisible = false;

    public ICommand ChangeBoolValue { protected set; get; }
    public ICommand Debug { protected set; get; }


    public bool SwitchVisible

    {



        get

        {

            System.Diagnostics.Debug.WriteLine("Debug: SwitchVisible has been fired " + isEditorVisible);
            return isEditorVisible;

        }



    }




    public HandleEditor()


    {


        ChangeBoolValue = new Command(() =>
        {


            if (isEditorVisible == true)

            {

                isEditorVisible = false;
                OnPropertyChanged("SwitchVisible");
            }

            else
            {

                isEditorVisible = true;
                OnPropertyChanged("SwitchVisible");

            }


        });


    }


    protected virtual void OnPropertyChanged(string propertyName)
    {
        System.Diagnostics.Debug.WriteLine("Debug: OnPropertyChanged has been fired " + propertyName + " : " + isEditorVisible);

        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public void Pin_Clicked(CustomPin pin)

    {


        isEditorVisible = true;
        OnPropertyChanged("SwitchVisible");


    }
1
so basically what you want is when you click the pin the entry should change visibility?FreakyAli
@G.hakim - that's correct, I want the pin info summary window to have a click event attached to it ( which is in my code sample) and for that to trigger the behaviour on the grid, or the Entry element, and change the visibilityJourneyman1234

1 Answers

0
votes

So I might miss something but I don't see the purpose of the isEditorVisible property.

I have successfully bound the Entry IsVisible parameter to the behavior, and can toggle it on a button

You are using an Editor and not an Entry. Plus, you are binding the Command property of the button to a Command in your ViewModelwhich is ChangeBoolValue. You are not using a Behavior<T>, or you missed it in your snipet :).

<Editor x:Name="TextEntry" Grid.Row="1" VerticalOptions="FillAndExpand" IsVisible="{Binding SwitchVisible, Mode=TwoWay}"/>  

So what you want to achieve is the Editor IsVisibleProperty to change according to SwitchIsVisible property, which one you are binding to, right ?

I would do something like that in the ViewModel instead of your actual implementation :

public event PropertyChangedEventHandler PropertyChanged;

public ICommand ChangeBoolValue { protected set; get; }
public ICommand Debug { protected set; get; }

private bool _switchVisible;
public bool SwitchVisible
{

    get => _switchVisible;
    set
    {            
      if(_switchVisible == value) return;
      _switchVisible = value;
      OnPropertyChanged("SwitchVisible");
    }
}


public HandleEditor()
{
    ChangeBoolValue = new Command(() =>
    {
       SwitchVisible = !SwitchVisible;

    });
}


protected virtual void OnPropertyChanged(string propertyName)
{
    System.Diagnostics.Debug.WriteLine("Debug: OnPropertyChanged has been fired " + propertyName + " : " + isEditorVisible);

    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public void Pin_Clicked(CustomPin pin)
{
   SwitchVisible = true;
}