✅ How to use IRelayCommand in CommunityToolkit.Mvvm with parameters?
I'm just learning WPF and also the community toolkit. I can get RelayCommands working OK if they're parameter-less, but struggling to implement a command bound to a Toggle Button to toggle a bool property on a databound control. My working approach to simple commands:
However, I have tried something similar for the ToggleButton:
The button and control xaml:
However, this renders the button inoperable (no rollover highlighting, command doesn't execute) - it works without the Command Binding.
Any ideas?
EDIT: I've just realised that I don't technically need a Command here, I can just bind the ToggleButton's IsChecked property to the property directly. However, I'm still intersted to know why the above broke the ToggleButton and how to send parameters via cmmands using the Community Toolkit.
35 Replies
Community toolkit uses source generators
Note that the class must be declared as partial
The source generator will then generate a property called
FooCommand
which you can use in your XAML code
https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/relaycommand#command-parametersThanks for the reply. Have just tried it in my code, but still the ToggleButton is not working:
BUtton starts working if I remove the Command
Also tried this, but no dice:
u're not defining a parameter
so it breaks the toolkit apparently
so u either make it
Or you pass in a parameter
I think the only exception is if ur passing a CT
thanks for the reply, however:
doesn't work either.
it has parameter I just explained above
So I'm trying:
but weirdly it won't compile. It's also not registering the error in the error window
Do I have to define the TreuValue property as well?
no CTK should do it for u
I assume u are using it by the RelayCommand attribute and xaml code
just removed the code and it compiles fine.
what part u removed
my xaml is aper that in the OP -
yes I was referring to
Binding TestRCCommand
since u have RelayCommand and the xaml like that I assumed u are using CTK
well that should compiletry this
ah wait
The name 'TrueValue' does not exist in the current context
well CTK generates it for u
hover your mouse over ObservableProperty
and it will show u how it works
Try this instead
That did it - wow - so I have to pass all that in - I wonder why it doens't work with just [RelayCommand] without the can execute?
I mean it works
hah - well yeah
u can do just
but if u want to disable the button yada yada
u need something to test against
and the way u had it
with the parameter
but without having a parameter in the xaml
it breaks it
That's the original problem - just that was rendering the button unusable
shouldn't thou
its not checking against anything
ikr
I mean
its a toogle button
so it would go into disabled
but u should still be able to toggle it
ah yeah - let me try and re-author
naw - i give up - tried the below but button still not working:
WPF's great, but it is like talking with the world's largest pedant....
thanks for your help though - found a work around
😕
not sure what to tell u, its the exact same xaml u posted above
and the exact same method
then god knows
baffles me, wpf
try removing the element inside
and see if it works
because on mine I just used an image since I dont have that lib
maybe that lib hide hittest or something
which would make sense
Still no dice - but have found that button is disabled. If I remove the Command attribute in live editor, then the button is enabled again - thus it is something to do with the CanExecute part of the RrelayCommand. No idea why it's defaulting to false
nemmind - I'll use the workaround for now
thanks again 👍
Have you tried something like
Although, the CanExecute becoming false will no longer allow you to execute that command to turn it back to true, so instead you could try
Thanks @Auger - yeah we were there a number of posts ago - no dice
Specifically the
Mode=TwoWay
part may help if you plan to set that value from code and from the UI toggleah! you may have apoint there
There is also a dialog window in VS that is "XAML Binding Failures" I think. That's a good one to keep your eye on while running the app