Hello,
i need to enable/disable a button using ViewModel, please provide me a link which shows how to achieve it.
Here is a document about Data binding and MVVM - .NET MAUI | Microsoft Learn, you can refer to it.
Based on your description, you can add property BtnIsEnabled
, if you want to make the value of BtnIsEnabled
changed at the runtime, please implement the INotifyPropertyChanged interface like following code.
public class MyViewModel: INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string name = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
private bool _btnIsEnable;
public bool BtnIsEnabled
{
get { return _btnIsEnable; }
set { _btnIsEnable = value; }
}
public MyViewModel()
{
}
}
Then you can bind this BtnIsEnabled property in your button's IsEnabled
attribute.
<Button IsEnabled="{Binding BtnIsEnabled}" ></Button>
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.