Reputation: 59
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
}
Upvotes: 0
Views: 320
Reputation: 18861
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
{
//...
}
}
Upvotes: 1