C
C#ā€¢2y ago
1FriendlyDoge

ā” Issue with NotifyCanExecuteChangedFor from MVVM CommunityTK

The problem occurs in my view model (Avalonia MVVM), I set a variable
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(PreCheckCommand))]
public string username;
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(PreCheckCommand))]
public string username;
and try to run my function
[RelayCommand]
public void PreCheck() { ... }
[RelayCommand]
public void PreCheck() { ... }
whenever the text in to box is changed, it never executes though. Anyone has an idea what the issue is? This is how I use the binding in my xaml
<TextBox ... Text="{Binding Username, Mode=TwoWay}"></TextBox>
<TextBox ... Text="{Binding Username, Mode=TwoWay}"></TextBox>
27 Replies
Starlk
Starlkā€¢2y ago
Did you set the data-context for the view?
1FriendlyDoge
1FriendlyDogeā€¢2y ago
Other bindings work, so yeah
Starlk
Starlkā€¢2y ago
what UI framework is this?
1FriendlyDoge
1FriendlyDogeā€¢2y ago
Avalonia MVVM
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
twiner
twinerā€¢2y ago
Notification is for properties not fields In wpf atleast
1FriendlyDoge
1FriendlyDogeā€¢2y ago
Everything besides my function executing works, values update etc, so notification isnt the issue
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
1FriendlyDoge
1FriendlyDogeā€¢2y ago
And how do I get that?
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
1FriendlyDoge
1FriendlyDogeā€¢2y ago
not there for me, do I have to put it into the constructor or something? Just typing it in the class doesnt work for me, although the object exists
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
1FriendlyDoge
1FriendlyDogeā€¢2y ago
I set the var to private, didnt change anything
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
1FriendlyDoge
1FriendlyDogeā€¢2y ago
partial
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
1FriendlyDoge
1FriendlyDogeā€¢2y ago
It inherits from ObservableObject
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
1FriendlyDoge
1FriendlyDogeā€¢2y ago
Oh, it needed partial void
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
1FriendlyDoge
1FriendlyDogeā€¢2y ago
That finally fixed it, wow, 2 hours wasted :( thank you a lot for helping me out!
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
1FriendlyDoge
1FriendlyDogeā€¢2y ago
Yeah, I would have never guessed this, pretty weird way to go about this in my opinion...
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
333fred
333fredā€¢2y ago
/cc @Sergio
Sergio
Sergioā€¢2y ago
You're confusing things NotifyCanExecuteChangedFor doesn't invoke your command It just causes the command to signal its CanExecuteChanged event šŸ™‚ This was in reply to this šŸ˜„ If you want to run code whenever that changes, just do:
partial void OnUsernameChanged(string value)
{
// TODO: do stuff here
}
partial void OnUsernameChanged(string value)
{
// TODO: do stuff here
}
šŸ™‚
Accord
Accordā€¢2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.