XamarinForm DataTemplate ListView Binding

270 views Asked by At

I want to bind to a variable from inside a CustomControl referenced in a ListView DataTemplate

This is the situation: I have a custom control containing a ListView with many DataTemplates to display the various ViewCell (it's basically a set of dynamic fields that must be displayed according to their "type").

<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:s4gvForms="clr-namespace:S4GVMobile.Forms;assembly=S4GVMobile"
         xmlns:s4gvControls="clr-namespace:S4GVMobile.Controls;assembly=S4GVMobile"
         x:Class="S4GVMobile.Controls.AttributesList"
         x:Name="S4GVAttributesListControl"
         >
<ContentView.Resources>
    <ResourceDictionary>
        <!-- MANY DATA TEMPLATES HERE, ONLY ONE LEFT FOR REFERENCE -->
        <DataTemplate x:Key="s4gvAttributePictureTemplate">
            <ViewCell x:Name="s4gvAttributePictureViewCell">
                <ViewCell.View>
                    <StackLayout Orientation="Vertical">
                        <Label Text="{Binding Attribute.Key}" />
                        <s4gvControls:AttributePicture ParentID="{Binding Source={x:Reference s4gvAttributePictureViewCell}, Path=Parent.BindingContext.EntityID}" AttributeValue="{Binding .}"  />
                    </StackLayout>
                </ViewCell.View>
            </ViewCell>
        </DataTemplate>
        <!-- ... -->
        <!-- AGAIN, LONG LIST IN THE SELECTOR -->
        <s4gvForms:AttributeDataTemplateSelector x:Key="s4gvAttributeDataTemplateSelector"
                                                 AttributePictureTemplate="{StaticResource s4gvAttributePictureTemplate}"
                                                 />
    </ResourceDictionary>
</ContentView.Resources>
<StackLayout>
    <ListView x:Name="s4gvAttributes" HasUnevenRows="True" ItemTemplate="{StaticResource s4gvAttributeDataTemplateSelector}" ItemsSource="{Binding AttributeValues}" />
</StackLayout>

I'm passing to this custom control both the list of items (required by the ListView) and an additional EntityID.

<s4gv:AttributesList x:Name="s4gvSerialAttributes" AttributeValues="{Binding Serial.AttributeValues}" EntityID="{Binding Serial.SerialID}" VerticalOptions="FillAndExpand"/>

The custom control has the required bindable properties (AttibuteValues and EntityID) and relies on a dedicated ViewModel

I need to retrieve the EntityID from inside the DataTemplate (and put it in the ParentID property); this value is in the custom control's ViewModel but it is "outside" the ListView's DataTemplates. I've tried using the Parent.BindingContext (it works on Command and CommandParameter) but it seems that it can only go up to the ListView's context and not higher though I need to move one further step up to the CustomControl itself.

<s4gvControls:AttributePicture ParentID="{Binding Source={x:Reference s4gvAttributePictureViewCell}, Path=Parent.BindingContext.EntityID}" AttributeValue="{Binding .}"  />

Any idea? I'm happy to restructure the whole thing if required.

0

There are 0 answers