Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

MVVM Bindings inside a ListView

NicolevonLorneNicolevonLorne USMember
edited March 2017 in Xamarin.Forms

Hi, I'm a Xamarin and XAML beginner and I'm pretty stuck. For some reason when I use a binding to set a property on a custom view it just doesn't work at all.

In the code sample below you can see I've tried a bunch of different variations to try to determine why this isn't working and everything else works perfectly. Thus as far as I can tell my bindings are correct there must just be something I'm missing.

Can anyone shed some light?

    <ListView x:Name="vesselList" ItemsSource="{Binding Vessels}"
              <ListView.ItemTemplate Source="">
                    <!--WORKS: Vessel View (simplifed) and refactored out-->
                    <Frame OutlineColor="Accent">
                      <StackLayout Orientation="Horizontal" BackgroundColor="Teal">
                          <Label x:Name="name" VerticalOptions="CenterAndExpand" Text="{Binding .Name}"/>

                    <!--WORKS: calling vessel view and hardcoding property-->
                    <local:VesselView Name="HARDCODED NAME" />

                    <!--WORKS: Just displaying name in a label -->
                    <Label Text="{Binding .Name}"

                    <!--BREAKS: Setting a property with a binding-->
                    <local:VesselView Name="{Binding .Name}" />

Best Answer


  • NicolevonLorneNicolevonLorne USMember

    Thanks @ClintStLaurent , the issue wasn't actually in my usage of .Name (although I will make it more explicit maybe later?)

    I did however find what I was missing. I must remember to visit that site!

    In my VesselView I wasn't binding properly in the Xaml.
    So previously (wrong):

        <Label x:Name="name" VerticalOptions="CenterAndExpand"/>
        public static readonly BindableProperty NameProperty =
                BindableProperty.Create("Name",             // propertyname
                                        typeof(string),     // returntype
                                        typeof(VesselView), // declaringtype
                                        "Bindable Name"     // defaultValue
            public string Name
                    return (string)GetValue(NameProperty);
            name.Text = value;
                    SetValue(NameProperty, value);

    Correct method is to get rid of the name.Text in the setter and implement the xaml as:

        <ContentView xmlns="" 
            <Label x:Name="name" VerticalOptions="CenterAndExpand" 
                   Text="{Binding Source={x:Reference this}, Path=Name}"/>
  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Glad you got it resolved. Hurray!

  • NicolevonLorneNicolevonLorne USMember

    Tell me about it! I was doing my head in because I knew I was just missing something small that I didn't know yet! :s

  • NMackayNMackay GBInsider, University admin

    ItemsSource="{Binding Vessels}"

    I see it's not just me writing Xamarin apps for the maritime industry :smile:

  • NicolevonLorneNicolevonLorne USMember

    @NMackay hah! Vessels are important :)

Sign In or Register to comment.