✅ Binding a RelayCommand in a CollectionView DataTemplate with an other object as DataType
So I have following setup:
ViewModel as ObservableObject containing an ObservableCollection of an Object Called ToDo
The VM also has 3 RelayCommands being Add, Remove and SetDone. The Add one works fine as it's a simple button outside of my viewcollection
On my SwipeItems, I want the have the SetDone command from my VM and the Remove of my VM as binding.
This code is based on .Net's own video
https://www.youtube.com/watch?v=5Qga2pniN78
dotnet
YouTube
.NET MAUI Data Binding with MVVM & XAML [5 of 8] | .NET MAUI for Be...
Learn more ➡️ https://learn.microsoft.com/training/paths/build-apps-with-dotnet-maui/
Welcome to the .NET MAUI for Beginners Series where you will learn the basics of building multi-platform apps with .NET MAUI for iOS, Android, macOS, and Windows from a shared C# code base. In this video, James introduces us to the MVVM (Model-View-ViewModel) ...
12 Replies
I've never liked
RelativeSource
for that. I tend to give the root view a name, then use ElementName
:
it doesn't seem to recognise ElementName
Boo, must be a MAUI thing
I'm afraid so
Does setting the AncestorType to CollectionView and the Path to DataContext.SetDone work? I’ve used that in WPF but not sure on Maui
I'm sorry I don't understand what you mean
I just tested it, and it wouldn't work like I've done it in WPF. Are you using the MVVMCommunity Tookit?
Either way, using [RelayCommand] from the toolkit wouldn't execute the command, using your binding code, but manually declaring the command did:
Actually I looked at it again just after posting that, and it will work with [RelayCommand], I forgot you have to bind it in XAML to SetDoneCommand and RemoveCommand respectively as that's what the commands are called when code is generated by the toolkit. If you change those in your XAML all should work.
Yeah I am using the toolkit
Let’s try
Yeah so changing the Path from Path=SetDone to Path=SetDoneCommand should work
omfg
yeah so that solved it Well Thank you very much for the help!
np