简体   繁体   中英

How to check if a BindableProperty is being used?

I have a custom control where I create different BindableProperties . Is there a way to check if a BindableProperty is being used?

I have tried this but it doesn't work:

public static readonly BindableProperty ExampleItemSourceProperty = BindableProperty.Create("ExampleItemSource", typeof(IList), typeof(Editor));

public IList ExampleItemSource{
  get {
    return (IList) GetValue(ExampleItemSourceProperty );
  }
  set {
    SetValue(ExampleItemSourceProperty , value);
  }
}

if (ExampleItemSourceProperty == null) {
  //do something

} else {

  //do something

}

We could implement it by the event propertyChanged of the BindableProperty .

A static property-changed callback method can be registered with a bindable property by specifying the propertyChanged parameter for the BindableProperty.Create method. The specified callback method will be invoked when the value of the bindable property changes.

   public static readonly BindableProperty ExampleItemSourceProperty = BindableProperty.Create("ExampleItemSource", typeof(IList), typeof(Editor), propertyChanged: OnExampleItemChanged);

    static void OnExampleItemChanged(BindableObject bindable, object oldValue, object newValue)
    {
        // this method will been called when we binding this property and set the value.
        var editor = bindable as Editor;

        if(newValue !=null)
        {
             //...
            ObservableCollection<Example> ExampleList = new ObservableCollection<Example>(); 
            editor.collectionView.ItemsSource = ExampleList; 
        }

        else
        {
            //...
        }

    }

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