C#C
C#3y ago
16 replies
Temptica

✅ 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.

<SwipeItem Text="Check" BackgroundColor="Green"
           Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:ToDoViewModel}}, Path=SetDone }" 
           CommandParameter="{Binding .}"/>
<SwipeItem Text="Delete"  BackgroundColor="Red"
           Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:ToDoViewModel}}, Path=Remove }"
           CommandParameter="{Binding . }"/>

This code is based on .Net's own video

https://www.youtube.com/watch?v=5Qga2pniN78
image.png
Screenshot_2023-11-26_191223.png
YouTubedotnet
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) ...
.NET MAUI Data Binding with MVVM & XAML [5 of 8] | .NET MAUI for Be...
Was this page helpful?