Hello,
Welcome to our Microsoft Q&A platform!
To set binding for a property, it requires to specify the source. Please set BindingContext
for the page, it should be the page class itemself in above code.
public NotebooksVM()
{
...
BindingContext = this;
}
It's recommend to use a model class to create the properties for data binding. Here is the related code, you could refer to.
public class TestModel : INotifyPropertyChanged
{
bool testValue;
public TestModel(bool testValue)
{
TestCommand = new Command(testMethod);
this.testValue = testValue;
}
public ICommand TestCommand { get; set; }
private void testMethod(object obj)
{
if (testValue)
{
IconSource = "one";
}
else
{
IconSource = "grid_";
}
}
private string iconSource = "grid_";
public string IconSource
{
get
{
return iconSource;
}
set
{
if (iconSource != value)
{
iconSource = value;
NotifyPropertyChanged();
}
}
}
protected virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public partial class TestPage : ContentPage
{
TestModel model;
public TestPage()
{
InitializeComponent();
model = new TestModel();
BindingContext = model;
}
}
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.