You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(Code-Behind left out for brevity; MainViewModel injected and set as BindingContext in constructor; no other logic is present here)
MainViewModel.cs
usingObservableObject=CommunityToolkit.Mvvm.ComponentModel.ObservableObject;usingObservablePropertyAttribute=CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute;usingRelayCommandAttribute=CommunityToolkit.Mvvm.Input.RelayCommandAttribute;namespaceNamespace.ViewModels{publicpartialclassMainViewModel:ObservableObject{[ObservableProperty]privatebool_isbuttonEnabled;[...]publicMainViewModel(){IsButtonEnabled=false;}[...][RelayCommand]privatevoidButton(){// Execute Button Logic!// For instance:IsButtonEnabled=!IsButtonEnabled// Button disabled after 2 clicks!}[...]}}
Hooking up the bindable properties to the ContentPage and the respective ViewModel, the IsEnabled (set to false) appears to be ignored. If I explicitly update the value (toggle on, toggle back off), it DOES disable the button, but the initial state is not reflected (button is definitely enabled).
In the custom control, the Command is defined before the IsEnabled.
Same for the ContentPage it is the same, the command is bound before IsEnabled.
After some searching, I've seen some other resources that say that:
Order DOES matter, Command should be bound before IsEnabled (And if that is followed for a ContentPage without custom controls - e.g. one file definition - I have had success with this approach)
It seems as though one should use the CanExecute (from the ICommand) (This brings me to my second caveat below)
When using MVVM, I define my commands in my view model using the [RelayCommand] attribute (see above).
With this type of definition, how would I go about customizing the CanExecute aspect of this RelayCommand?
Naively, I would like to keep the Command as the logic for the action taken when the button is clicked, and to use IsEnabled as the logic to dictate whether or not the user CAN interact with the button at all.
Given the above, does anyone have any recommendations/suggestions/comments or documentation to provide, in an effort to find some solutions or to otherwise address these points of confusion/issues I have encountered?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Preface: I am using the
CommunityToolkit.Maui
andCommunityToolkit.Mvvm
NuGet
packagesI have encountered this issue a few times now, and was wondering what the recommended solution or documentation to follow would be.
I have a visual component (control), which has a
BindableProperty
for anICommand
for a button (binding to theCommand
property).I was hoping to also bind the
IsEnabled
for this button to aBindableProperty
.For example:
ViewWithButton.XAML
:(Code-Behind left out for brevity; random thought: would order matter here too for bindings?)
MainPage.XAML
(Code-Behind left out for brevity;
MainViewModel
injected and set asBindingContext
in constructor; no other logic is present here)MainViewModel.cs
Hooking up the bindable properties to the
ContentPage
and the respectiveViewModel
, theIsEnabled
(set tofalse
) appears to be ignored. If I explicitly update the value (toggle on, toggle back off), it DOES disable the button, but the initial state is not reflected (button is definitely enabled).In the custom control, the
Command
is defined before theIsEnabled
.Same for the
ContentPage
it is the same, the command is bound beforeIsEnabled
.After some searching, I've seen some other resources that say that:
Command
should be bound beforeIsEnabled
(And if that is followed for aContentPage
without custom controls - e.g. one file definition - I have had success with this approach)CanExecute
(from theICommand
) (This brings me to my second caveat below)When using MVVM, I define my commands in my view model using the
[RelayCommand]
attribute (see above).With this type of definition, how would I go about customizing the
CanExecute
aspect of thisRelayCommand
?Naively, I would like to keep the
Command
as the logic for the action taken when the button is clicked, and to useIsEnabled
as the logic to dictate whether or not the user CAN interact with the button at all.Given the above, does anyone have any recommendations/suggestions/comments or documentation to provide, in an effort to find some solutions or to otherwise address these points of confusion/issues I have encountered?
Many many thanks in advance!
Beta Was this translation helpful? Give feedback.
All reactions