listviewxamarin.formsbinding-context

Xamarin.Forms get ViewModel as BindingContext in ViewCell of ListView


I add DataTemplateSelector in FlowListView(Basically same as ListView).

<ContentView
    ...
    BindingContext="{Binding PatientImageListVM, Source={StaticResource VMLocator}}">
    ...
    <flv:FlowListView
        ...
        HasUnevenRows="true">
        <flv:FlowListView.FlowColumnTemplate>
            <local:PatientImageListDataTemplateSelector/>
        </flv:FlowListView.FlowColumnTemplate>
    </flv:FlowListView>
</ContentView>

And I use this code for get cell with binding.

button.SetBinding(Button.CommandParameterProperty, ".");

But I want to use some variables in ViewModel binded with ContentView in DataTemplateSelector. I tried this code but not working.

label.SetBinding(Label.IsVisibleProperty, 
    "{Binding BindingContext.LabelVisibility, Source={x:Reference Page}}");

How can I get a vatiable in ViewModel in DataTemplateSellector?


Solution

  • label.SetBinding(Label.IsVisibleProperty, new Binding("BindingContext.LabelVisibility", source: this));
    

    This should works. You should pass the "Page" to your binding with "this". So you should set your binding when you create the ItemTemplate

    lv.ItemTemplate = new DataTemplate(() =>
    {
        //....
        label.SetBinding(Label.IsVisibleProperty, new Binding("BindingContext.LabelVisibility", source: this));
    }