0
votes

view

viewmodel, model and code behind

Hello Community,

need help with data bindings recognizing the MVVM pattern in a ListView in XAML. I don't know why it doesn't work. I can't see any data on the view. I have got only this Exception through debugging.

Xamarin.Forms.Xaml.XamlParseException: Position 8:10. Property Content is null or is not IEnumerable

Thanks for answers.

XAML

  <ViewCell>
        <StackLayout HorizontalOptions="FillAndExpand" Orientation="Horizontal">
         <StackLayout HorizontalOptions="Start" Orientation="Vertical">
               <Label Text="{Binding Name}" />
          </StackLayout>
          <StackLayout HorizontalOptions="End" Orientation="Vertical">
                <Label Text="{Binding Information}" />
           </StackLayout>
     </StackLayout>
</ViewCell>

Code

public partial class PartyStatusPage : ContentPage
{
    public PartyStatusPage()
    {
        InitializeComponent();            
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        BindingContext = new PartyStatusPageViewModel();
    }
}

public class PartyStatusPageViewModel
{
    public List<PartyStatus> ItemList;

    public PartyStatusPageViewModel()
    {
        ItemList = new List<PartyStatus>();
        ItemList.Add(new PartyStatus { Name = "Max", Information = "test" });
    }
}

public class PartyStatus
{
    public string Name { get; set; }

    public string Information { get; set; }

    public bool State { get; set; }
}
2
Please dont use screens to show your code. Post your code here in an Code-Tag. Also, make your Source a property instead of a fieldlokusking
Oh sorry, I'm a new user. Thanks for the tip.Simon Waiblinger

2 Answers

0
votes

Your DataTemplate in your XAML is wrong. You need to specify the view of your ViewCell explicit. Look at the following XAML:

<ListView.ItemTemplate>
    <DataTemplate>
        <ViewCell>
            <ViewCell.View>
                <!-- Your template (StackLayout for example -->
            </ViewCell.View>
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>
0
votes

For example the XAML:

<ListView x:Name="listView" ItemsSource="{Binding Readycontacts}" SelectedItem="{Binding SelectedItem}"  Margin="10,10,94.234,10">
        <ListView.View>
            <GridView>
                <GridViewColumn    Width="130" Header="Name"
                    DisplayMemberBinding="{Binding Path=Name}" />
                <GridViewColumn Width="180" Header="Adresse"
                    DisplayMemberBinding="{Binding Path=Addresse}" />
                <GridViewColumn Width="130" Header="Telefonnummer"
                    DisplayMemberBinding="{Binding Path=Telefonnummer}" />
                <GridViewColumn Width="130" Header="Mobil"
                    DisplayMemberBinding="{Binding Path=Mobil}" />
            </GridView>
        </ListView.View>
    </ListView>

The ViewModel (you need to refresh your var's after changing the values)

[EDIT] or your View won't recognize/display anything [EDIT]

    private string _name = "";
    public string Name
    {
        get
        {
            return _name;
        }
        set
        _name = value;
            RaisePropertyChanged("Name");
        }
    }
RaisePropertyChanged("Name");

Will refresh your Binding.