Share via

x:bind how to refence viewmodel

Eduardo Gomez 4,316 Reputation points
2026-02-18T19:16:07.1633333+00:00
GridView
            Grid.Row="1"
            ItemsSource="{x:Bind ViewModel.Contacts}"
            SelectionMode="None">
            <GridView.ItemTemplate>
                <DataTemplate x:DataType="model:Contact">
                    <Border>
                        <Grid
                            Width="80"
                            Height="80"
                            Background="#0050EF">
                            <TextBlock
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                FontSize="20"
                                Text="{x:Bind InitialsName}" />
                        </Grid>
                        <interactivity:Interaction.Behaviors>
                            <interactivity:EventTriggerBehavior EventName="Tapped">
                                <interactivity:InvokeCommandAction Command="{}}" CommandParameter="{x:Bind}" />
                            </interactivity:EventTriggerBehavior>
                        </interactivity:Interaction.Behaviors>
                    </Border>
                </DataTemplate>
            </GridView.ItemTemplate>
            <GridView.ItemsPanel>
                <ItemsPanelTemplate>
                    <ItemsWrapGrid MaximumRowsOrColumns="4" Orientation="Horizontal" />
                </ItemsPanelTemplate>
            </GridView.ItemsPanel>
        </GridView>

in xbind how can do cammand in my vm

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using EasyShere.Models;
using EasyShere.Services;
using System.Collections.ObjectModel;
namespace EasyShere.ViewModel;
public partial class FriendsPageViewModel : ObservableObject {
    public ObservableCollection<Contact> Contacts { get; set; } = [];
    public FriendsPageViewModel() {
        Contacts = FrinedsService.GetContacts();
    }
    [RelayCommand]
    void OnContactTapped(Contact contact) {
    }
}
Windows development | WinUI
0 comments No comments
{count} votes

Answer accepted by question author
  1. Jack Dang (WICLOUD CORPORATION) 14,340 Reputation points Microsoft External Staff Moderator
    2026-02-19T03:38:45.5433333+00:00

    Hi @Eduardo Gomez ,

    Thanks for reaching out.

    When you set:

    <DataTemplate x:DataType="model:Contact">
    

    the binding context inside that template becomes Contact. So x:Bind can only “see” members of Contact, not your FriendsPageViewModel. That’s why referencing the command directly doesn’t work.

    Since you’re using [RelayCommand], the toolkit generates a property named OnContactTappedCommand. The key is making sure the template can access your page’s ViewModel.

    Below is an approach you can consider:

    1. Give your Page a name and expose the ViewModel
    <Page
        x:Class="EasyShere.Views.FriendsPage"
        x:Name="RootPage">
    

    (Assuming you already have a ViewModel property set on the page.)

    1. Reference the ViewModel explicitly from the template
    <interactivity:InvokeCommandAction
        Command="{x:Bind RootPage.ViewModel.OnContactTappedCommand}"
        CommandParameter="{x:Bind}" />
    

    Here’s what’s happening:

    • RootPage.ViewModel.OnContactTappedCommand tells x:Bind exactly where the command lives.
    • CommandParameter="{x:Bind}" passes the current Contact item to your command.

    That way, when an item is tapped, your method:

    void OnContactTapped(Contact contact)
    

    will be called with the correct contact.

    Take the code above as a reference and modify it to suit your project’s structure, naming conventions, and how you expose the ViewModel.

    Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Q&A Assist
    2026-02-18T19:16:17.0466667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To reference a command in your ViewModel using {x:Bind}, you can bind the Command property of the InvokeCommandAction to the command in your ViewModel. In your case, you have a command called OnContactTapped that you want to invoke when a contact is tapped. Here’s how you can modify your XAML to achieve this:

    <interactivity:InvokeCommandAction Command="{x:Bind ViewModel.OnContactTapped}" CommandParameter="{x:Bind}" />
    

    Make sure that your ViewModel is properly set as the DataContext for your page or control. This way, the x:Bind will correctly reference the OnContactTapped command in your FriendsPageViewModel.

    Here’s the modified part of your XAML:

    <interactivity:EventTriggerBehavior EventName="Tapped">
        <interactivity:InvokeCommandAction Command="{x:Bind ViewModel.OnContactTapped}" CommandParameter="{x:Bind}" />
    </interactivity:EventTriggerBehavior>
    

    This ensures that when a contact is tapped, the OnContactTapped method in your ViewModel is called with the tapped contact as the parameter.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.