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

Multiple datatemplates binding

$
0
0

Hi, I am sorry if the question is stupid...

I am trying to use multiple DataTemplates in ListView. The Table binding goes well, whereas the Conversation not... The error message is "No property, bindable property, or event found for Name" (I have the property Name in the ConversationUserControl).

The ItemsSource is a list of objects inherited from the same abstract class.

Maybe the problem is in binding inside the ResourceDictionary, but I don't understand why it works for the first DataTemplate and not for the second one :(
Please help.

.... 
   <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="Table">
                <ViewCell>
                    <local:TableUserControl Table="{Binding Table}"/>
                </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="Conversation">
                <ViewCell>
                    <local:ConversationUserControl Name="{Binding Name/*  DOESN'T WORK HERE */ }"/>
                </ViewCell>
            </DataTemplate>            
            <local:ContentTemplateSelector x:Key="ContentTemplateSelector"
                                           TableContentTemplate="{StaticResource Table}"
                                           ConversationContentTemplate="{StaticResource Conversation}"/>
        </ResourceDictionary>
    </ContentPage.Resources>

..
    <ContentPage.Content>
        <ListView ItemsSource="{Binding Contents}"
                  ItemTemplate="{StaticResource ContentTemplateSelector}"
                  HasUnevenRows="True" >
        </ListView>
    </ContentPage.Content>

GridUserControl class (the ConversationUserControl is almost the same, only the property is of type Conversation and OnTableChanged method is different too):

public partial class TableUserControl : Grid
{
    public TableUserControl ()
    {
        InitializeComponent ();                                   
    }

        public static readonly BindableProperty TableProperty =
            BindableProperty.Create(nameof(Table), typeof(MyTable), typeof(TableUserControl), null,
                BindingMode.OneWay, null, OnTableChanged);

        public MyTable Table
        {
            get { return (MyTable)GetValue(TableProperty); }
            set  {   SetValue(TableProperty, value);  }
        }

        private static void OnTableChanged(BindableObject bindableObject, object oldobject, object newobject)
        {
              . . . . . .
        }                    
    }

Viewing all articles
Browse latest Browse all 77050

Trending Articles



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