-1

I have a data template with an image which has Source property binding to the default source(the observable collection, it works fine). The issue is that I need its IsVisible property binding to other source(an object declared in my code behind) but when running app I'm getting this on console:

Binding: 'ScrollEvent' property not found on 'Xamarin.Forms.Binding', target property: 'FFImageLoading.Forms.CachedImage.IsVisible'

Relevant parts of my code:

MyPage.xaml

<DataTemplate x:Key="MapMsgSend">

 ...

   <ffimageloading:CachedImage
        Source="{Binding imageSource}" 
        IsVisible="{Binding Source={Binding MyPage}, Path=ScrollEvent.Visibility}">
   </ffimageloading:CachedImage>

 ...

</DataTemplate>

MyPage.xaml.cs (related part)


namespace Project.XAML
{
    public partial class MyPage : ContentPage
    {
       public MyPage(){
         this.BindingContext=this;
       }
       public IsScrolling ScrollEvent = new IsScrolling() { ShowImage = true };
    } 
}

EDIT

public class IsScrolling : INotifyPropertyChanged
{
      private bool _ShowImage { get; set; }
      public bool ShowImage
      {
          get { return _ShowImage; }
          set
          {
              _ShowImage = value;
              NotifyPropertyChange("ShowImage");
          }
      }

      public event PropertyChangedEventHandler PropertyChanged;
      void NotifyPropertyChange(string PropName)
      {
          if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(PropName));
      }
}

//default value
public IsScrolling ScrollEvent = new IsScrolling() { ShowImage = true };
Éder Rocha
  • 1,538
  • 11
  • 29

1 Answers1

1

the syntax should be something like this

<ContentPage ... x:Name="page">
...

<ffimageloading:CachedImage
    Source="{Binding imageSource}" 
    IsVisible="{Binding Source={x:Reference page},Path=ScrollEvent.Visibility}"

you can only bind to public properties

this is public, but it is not a property

public IsScrolling ScrollEvent = new IsScrolling() { ShowImage = true };

it needs get and/or set to be a property, like this

public IsScrolling ScrollEvent { get; set; } = new IsScrolling() { ShowImage = true };
Jason
  • 86,222
  • 15
  • 131
  • 146
  • I don't have any doubt that you know how to do this, you helped me a lot of times already. But I was doing exactly as you said. I just moved it back now. It crashs with exception: ```Xamarin.Forms.Xaml.XamlParseException: Position 282:57. Can not find the object referenced by `Page` at Xamarin.Forms.Xaml.ReferenceExtension.ProvideValue (System.IServiceProvider serviceProvider) [0x000bf] in D:\a\1\s\Xamarin.Forms.Xaml\MarkupExtensions\ReferenceExtension.cs:42 at MasterDetailPageNavigation.XAML.Page+_anonXamlCDataTemplate_8.LoadDataTemplate () [0x00a2c] in ``` – Éder Rocha Jun 20 '20 at 23:26
  • I will accept your answer since it might be the right way to do it, but if you could help me a little bit more in finding why it's crashing I would be very happy. – Éder Rocha Jun 20 '20 at 23:32
  • the error message is pretty clear - "Position 282:57. Can not find the object referenced by `Page`". Are you using consistent naming and casing with your names? You might want to use a different name, as "Page" might conflict with some built in types. – Jason Jun 20 '20 at 23:52
  • actually, the name is ChatPage, I renamed to post the question and keep it to don't cause confusion. And of course, it exists... it's the same page where I want to do the bind. – Éder Rocha Jun 20 '20 at 23:54
  • does the name you use in `x:Name` match the name you use in `x:Reference`? – Jason Jun 21 '20 at 00:01
  • I didn't give any x:Name to this Page. Do I have to do it? Even if the object to binding to is declared only in code behind? – Éder Rocha Jun 21 '20 at 01:46
  • I have just did it but same problem: Binding: 'ScrollEvent' property not found on 'MasterDetailPageNavigation.XAML.ChatPage'. And yes, I'm giving same name to x:Reference. I think a workaround will be setting a Label or other view that binds its visibility to ScrollEvent and than do ``````. This should work since I can set Label binding context directly in code behind. – Éder Rocha Jun 21 '20 at 01:56
  • ScrollEvent needs to be a public property – Jason Jun 21 '20 at 02:14
  • It is... public IsScrolling ScrollEvent – Éder Rocha Jun 21 '20 at 02:16
  • No it is not. Please lookup C# properties – Jason Jun 21 '20 at 02:20
  • Well, this is what I have: ```public IsScrolling ScrollEvent = new IsScrolling() { ShowImage = true }; ``` – Éder Rocha Jun 21 '20 at 02:51
  • Properties require a get and/or set – Jason Jun 21 '20 at 03:03
  • I've just edited question with implementation. Is it wrong? – Éder Rocha Jun 21 '20 at 03:19