Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

Databinding to a command inside a DataTemplate

$
0
0

Hi there, I'm just having a play with Xamarin Forms and databinding and am having a problem working out the syntax. Say I've got a view model associated with some XAML. Inside the view model I've got a class Foo

    class Foo
    {
            public string Name {get; set;}
            public string Details {get; set;}
    }

I've also got a command

    public ICommand DoSomethingCommand { protected set; get; }

and an observable collection of Foos

    private ObservableCollection<Foo> _foos = new ObservableCollection<Foo>();
    public ObservableCollection<Foo> Foos
            {
                get { return _foos; }
            }

In my XAML, I've got a ListView. The binding to each field of my Foo works fine. However, I want to bind a command to each row, so that I can do something when the user taps on the row. The binding doesn't work though, presumably because the command isn't inside the Foo object? I've done some checking online and people suggest using RelativeSource to get to the command object but this doesn't work (I get a runtime xaml parse error). Anyone got any solution to this?

<ListView x:Name="FooView" ItemsSource="{Binding Foos}">
    <ListView.ItemTemplate>
      <DataTemplate>

                    <TextCell Text="{Binding Name}" Detail="{Binding Details}" Command="{Binding DoSomethingCommand}" />

      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

Viewing all articles
Browse latest Browse all 89864

Trending Articles