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

How to bind a collection to a custom view.

$
0
0

I want to make a custom view as it is used a bunch of places.
In essence, it is just a comment label, and a stacklayout with labels containing tags categorizing the comment.

[somewhat simplified]
<StackLayout>
    <Label x:Name="commentLabel"/>
    <!--The commentStackLayout should contain Labels with tags-->
    <StackLayout x:Name="commentStackLayout" Orientation="Horizontal"/>
</StackLayout>

In my viewController, I have an observable collection of strings which I would like to databind to this component in such a fashion that the commentStackLayout will contain individual labels coresponding to each string in the collection.

Anyone know how to do this?

What I have done:

I have made the commentLabel bindable in the normal fashion like this:

public static BindableProperty CommentProperty = BindableProperty.Create(
            propertyName: "Comment",
            returnType: typeof(string),
            declaringType: typeof(MyComponent),
            defaultValue: "",
            defaultBindingMode: BindingMode.OneWay,
            propertyChanged: HandleCommentChanged);

private static void HandleCommentChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var detailHeader = (MyComponent)bindable;
            detailHeader.commentLabel = newValue.ToString();
        }

public string Comment
        {
            get { return (string)GetValue(CommentProperty); }
            set { SetValue (CommentProperty, value); }
        }

I am however unsure how to bind the collection. I assume that I can flush all the children of the commentStackLayout on changes and add all the strings as labels, but I am unsure how to manually bind to a collection and not just primitives.

Any help would be appriciated.


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>