具有来自两个来源的数据绑定的DataTemplate

时间:2020-06-20 17:46:18

标签: c# xamarin xamarin.forms data-binding xamarin-binding

我有一个带有图像的数据模板,该图像的Source属性绑定到默认源(可观察的集合,它工作正常)。问题是我需要将其IsVisible属性绑定到其他源(在我的代码后面声明的对象),但是在运行应用程序时,需要在控制台上获取它:

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

我代码的相关部分:

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(相关部分)


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

编辑

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

1 个答案:

答案 0 :(得分:1)

语法应该是这样

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

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

您只能绑定到公共属性

这是公开的,但不是财产

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

它需要get和/或set才能成为属性,像这样

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