Skip to content

How to call command from a data template? #17771

Closed Answered by nor0x
Kirk-iOS asked this question in Q&A
Discussion options

You must be logged in to vote

you could also use x:Reference in the Binding Source and reference the element which has the ViewModel as a BindingContext

given you have a ContentPage with a Command in the BindingContext

<ContentPage
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:vm="clr-namespace:MyProject.ViewModels"
    x:Name="PageSelf"
    x:DataType="vm:MyViewModel"
    x:Class="MyProject.MyPage">

...

<!-- inside DataTemplate -->
Command="{Binding Source={x:Reference PageSelf}, Path=BindingContext.MyCommand}"

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
3 replies
@Kirk-iOS
Comment options

@sjordanGSS
Comment options

@matmork
Comment options

Comment options

You must be logged in to vote
3 replies
@Kirk-iOS
Comment options

@sisaacks
Comment options

@amadzarak
Comment options

Answer selected by Kirk-iOS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants