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}"
            HasUnevenRows="True">
              <ListView.ItemTemplate Source="">
                <DataTemplate>
                  <ViewCell>
                    <!--WORKS: Vessel View (simplifed) and refactored out-->
                    <Frame OutlineColor="Accent">
                      <StackLayout Orientation="Horizontal" BackgroundColor="Teal">
                          <Label x:Name="name" VerticalOptions="CenterAndExpand" Text="{Binding .Name}"/>
                      </StackLayout>
                    </Frame>

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

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

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

Best Answer

Answers

  • 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
            {
                get
                {
                    return (string)GetValue(NameProperty);
                }
    
                set
                {
            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="http://xamarin.com/schemas/2014/forms" 
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
                 x:Class="Views.VesselView"
                 x:Name="this">
    
            <Label x:Name="name" VerticalOptions="CenterAndExpand" 
                   Text="{Binding Source={x:Reference this}, Path=Name}"/>
        </ContentView>
    
  • 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 mod

    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.