I have an ObservableCollection
as the ItemsSource
for a ListView
.
Adding items works fine and the ListView
updates, but in the ListView
all I can see is this:
What am i missing?
public class MenuItem
{
public string Item { get; set; }
public string Price { get; set; }
}
// ...
public ObservableCollection<MenuItem> MenuItemList = new ObservableCollection<MenuItem>();
// ...
MenuItemList.Add(new MenuItem {Item = MenuData[6].InnerText, Price = PriceData[6].InnerText});
// ...
listViewFood.ItemsSource = MenuItemList;
CodePudding user response:
You created a custom type MenuItem
. The ListView
has no way of knowing how to display items of this type, that is why it displays the result of ToString
, which is the type name here. You have to define the appearance of an item yourself using a DataTemplate
as ItemTemplate
, e.g.:
<ListView x:Name="listViewFood">
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type MenuItem}">
<StackPanel>
<TextBlock Text="{Binding Item}"/>
<TextBlock Text="{Binding Price}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
For more information see the Data Templating Overview, which covers the mechanics in detail.