Search code examples
c#xamarinxamarin.formsdata-bindingxamarin-binding

DataTemplate with data binding from two sources


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 };

Solution

  • 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 };