简体   繁体   中英

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 ?

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM