ListView not binding

LukeConnerLukeConner USMember, University ✭✭

I'm just switching over to using Xamarin forms, so I feel like this is something simple I'm missing.

Here is my listview

<StackLayout Orientation="Vertical" Padding="20"> <SearchBar x:Name="CodeSearch"></SearchBar> <ListView x:Name="SearchList" ItemsSource="{Binding CurrentCodes}"> <ListView.ItemTemplate> <DataTemplate> <ViewCell> <Label Text="test" /> <Label x:Name="CodeLabel" Text="{Binding Code}" /> <Label x:Name="DescriptionLabel" Text="{Binding ShortDescription}" /> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout>

And here is the CurrentCodes
public IEnumerable<Icd10Code> CurrentCodes { get { return codeArray.Count == 0 ? new List<Icd10Code>() : codeArray [arrayIndex]; } }

When the app loads though it displays an empty table. The test label was just to see if it was the label that wasn't binding, but the test label doesn't show either.

When the CurrentCodes changes value will the listview update on it's own, or do I need to trigger it somehow?

Best Answer


  • stvansolanostvansolano UMInsider, University ✭✭✭

    Hey @LukeConner are you setting somehow the BindingContext property for your control? It looks like the missing piece there since it should hold the CurrentCodes

  • LukeConnerLukeConner USMember, University ✭✭
    edited August 2015

    I saw the BindingContext mentioned, but I don't understand how it fits in. I couldn't find any examples of it.
    I've tried doing SearchList.BindingContext = CurrentCodes;

    But I'm not sure that's how you're supposed to do that.

    What's the difference between the ItemsSource and the BindingContext?

  • LukeConnerLukeConner USMember, University ✭✭
    edited August 2015

    Your SearchList.BindingContext = this; worked.

    I initially put it in the wrong spot.

  • stvansolanostvansolano UMInsider, University ✭✭✭

    Great! Ready to go then.

  • arroyosarroyos MYMember
    edited August 2015

    @LukeConner , personal suggestion.

    Code your UI in XAML . personally find it easier to do binding/mvvm stuffs with xaml based UI .

    Also more example on these areas in the internet (xaml based examples).

Sign In or Register to comment.