AgentD
AgentD

Reputation: 1

How to access local variable from XAML using Xamarin

I should know this but can't find the right information. I'm trying to get my Xamarin.Forms ListView to work and I need to set the ItemsSource to my local variable named "theHerd" which is of type ObsverableCollection. Here is what I have, how do I need to change it to make it work?

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:HerdViewerPrototype" x:Class="HerdViewerPrototype.HerdViewerPrototypePage">

<ListView x:Name="lstview" ItemsSource="{local:theHerd}" />

</ContentPage>

Upvotes: 0

Views: 3562

Answers (3)

Yuri S
Yuri S

Reputation: 5370

Here is how you do it in xaml:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:ButtonRendererDemo;assembly=ButtonRendererDemo"
             x:Class="ButtonRendererDemo.ImageTapComplexPage"
             BindingContext="{StaticResource viewModel}">

      <ContentPage.Resources>
        <ResourceDictionary>
          <local:YourViewModelClassType x:Key="viewModel"/>
        </ResourceDictionary>
      </ContentPage.Resources>

Then you can use either

 <ListView x:Name="lstItems" RowHeight="60" ItemsSource="{Binding Items}" >

which takes model from the top of the page binding or

<TapGestureRecognizer Command="{Binding Source={StaticResource viewModel}, Path=TapCommand}" CommandParameter="{Binding name}" />

which takes the model from static resource

Upvotes: 1

danhomp
danhomp

Reputation: 46

I think you are mixing a couple of things here.

The right way to do this is as Wilson said. Use MVVM Pattern and set Page's binding context to the ViewModel created for that purpose.

Anyway, even if you want to do all the things from xaml, you always have to set the Page's BindingContext to something. So in code behind you will have

this.BindingContext = this;

And then in xaml:

<ListView x:Name="lstview" ItemsSource="{Binding theHerd}" />

As I said, it is weird and is not the recommended way of doing this.

Upvotes: 0

Wilson Vargas
Wilson Vargas

Reputation: 2899

You should user MVVM pattern

And call this ObservableColletion from XAML like this:

In your XAML code:

<ListView ItemsSource="{Binding Items}"
          CachingStrategy="RecycleElement"
          RowHeight="60">

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Margin="8">

                    <Label Text="{Binding Make}"
                           FontAttributes="Bold" />
                    <Label Text="{Binding YearOfModel}" />
                </StackLayout>
            </ViewCell>
        </DataTemplate>
</ListView.ItemTemplate>

And you should create a ViewModel class like this:

public class CarsViewModel : INotifyPropertyChanged {

    private ObservableCollection<Car> items;
    public ObservableCollection<Car> Items {
        get { return items; }
        set {

            items = value;
        }
    }


    public CarsViewModel() {

        // Here you can have your data form db or something else,
        // some data that you already have to put in the list
        Items = new ObservableCollection<Car>() {
            new Car()
            {
                CarID = 1,
                Make = "Tesla Model S",
                YearOfModel = 2015
            },
              new Car()
            {
                CarID = 2,
                Make = "Audi R8",
                YearOfModel = 2012
            },

        };
    }
}

Finally, in you MainPage.xaml.cs like this:

BindingContext = new CarsViewModel();

Upvotes: 1

Related Questions