Reputation: 12237
This is very basic, not sure why DisplayMemberPath is not showing, it just shows the class names.
<ListView x:Name="lv" ItemsSource="{Binding ElementName=root, Path=ShoppingList}" DisplayMemberPath="Name">
<ListView.View>
<GridView>
<GridViewColumn Header="Item" Width="150"/>
</GridView>
</ListView.View>
</ListView>
where root
is the name of Window.
public partial class MainWindow : Window
{
public ObservableCollection<Product> ShoppingList { get; set; }
public MainWindow()
{
ShoppingList = new ObservableCollection<Product>();
ShoppingList.Add(new Product() { Name = "Coffee", Price = 4, Info = "Out of stock" });
ShoppingList.Add(new Product() { Name = "Donut", Price = 2, Info = "Buy one get one free" });
ShoppingList.Add(new Product() { Name = "Sandiwich", Price = 7, Info = "Comes with free Coffee" });
InitializeComponent();
}
}
Not sure what am I missing?
public class Product
{
public String Name { get; set; }
public int Price { get; set; }
public String Info { get; set; }
}
Upvotes: 0
Views: 89
Reputation: 128077
From the Remarks in the DisplayMemberPath
documentation:
This property is a simple way to define a default template that describes how to display the data objects.
This won't work in conjunction with a GridView, where you set the DisplayMemberBinding
property for the template of each column:
This property associates a type of data from a data source to a column so that the column displays a set of values of that type.
<ListView ItemsSource="{Binding ElementName=root, Path=ShoppingList}">
<ListView.View>
<GridView>
<GridViewColumn Header="Item" Width="150"
DisplayMemberBinding="{Binding Name}"/>
</GridView>
</ListView.View>
</ListView>
Or you don't set the ListView's View
property at all:
<ListView ItemsSource="{Binding ElementName=root, Path=ShoppingList}"
DisplayMemberPath="Name"/>
But then you would usually not use a ListView, but the simpler ListBox.
Upvotes: 1