StriBog
StriBog

Reputation: 175

Xamarin How to create customCell with custom data

As i'm understood,we use the method "SetBinding" for to obtain data But if i'm use a custom class for data, i havn't that method. How i can extend the my class for that?

var image = new Image();
var nameLabel = new Label();
var typeLabel = new Label();

//set bindings
nameLabel.SetBinding(Label.TextProperty, new Binding("Name"));
typeLabel.SetBinding(Label.TextProperty, new Binding("Type"));
image.SetBinding(Image.SourceProperty, new Binding("Image"));

my class:

public class TextTable
    {
        public string Name { get; set; }
        public string[] Column { get; set; }
        public DataFormat[] Data { get; set; }
     }

Upvotes: 0

Views: 80

Answers (2)

Axemasta
Axemasta

Reputation: 833

Firstly you should really consider doing your UI in XAML, it keeps a good separation of concerns (UI & Data etc) and makes binding incredibly easy (compare to code behind).

Whilst not the same as your question, I posted an answer here which is similar to your question and would be worth reading.

I'll post an example of a complete data binding scenario (with a custom object), however bear in mind your question concerns basic data binding principles. There are many online resources that you should probably go and look up, I'll start you off on the data binding docs for xamarin.

The Model:

public class MyObject
{
    public string Title { get; set; }
    public string Description { get; set; }
    //This class can have any property you want
}

I wanted to display this data on a list view:

<ListView ItemsSource="{Binding TheItemSource}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextCell Text="{Binding Title}" Detail="{Binding Description}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

I am binding this ListView to a public ObservableCollection<MyObject>, once I have set this, I can bind my UI to any of the properties defined under MyObject.

In your view model you will need to have a property to bind to, in this case we need an ObservableCollection (we can also use List).

private ObservableCollection<MyObject> _theItemSource;
public ObservableCollection<MyObject> TheItemSource
{
    get
    {
        return _theItemSource;
    }
    set
    {
        //Your view model will need to implement INotifyPropertyChanged
        //I use prism for MVVM so I have a different method than normal to notify the view that a property has changed (its normally OnPropertyChanged()).
        SetProperty(ref _theItemSource, value);
    }
}

Now in the ViewModel you should set the value of _theItemSource which will then be used when the list view asks for the value of TheItemSource.

At this point you can populate your list with data & it will be presented on the list view that we defined in XAML earlier.

I once again strongly urge you to create your UI in XAML, it makes binding much much easier!

Upvotes: 1

StriBog
StriBog

Reputation: 175

SetBinding is a UI object's method

Upvotes: 0

Related Questions