i have a class called Robot
:
public class Robot
{
public string name { get; private set; }
public Robot(string robotName)
{
name = robotName;
}
}
I made in my ModelView an ObservableCollection of this class:
public ObservableCollection<Robot> Robots { get; private set; } = new ObservableCollection<Robot>();
And I need now to bind this ObservableCollection to my ListView, but I need the property name
binded to the ListView, not the class converted to a string.
<ListView ItemsSource="{Binding Robots}" />
How do I do this?
CodePudding user response:
Set the DisplayMemberPath
property to the name of the item property:
<ListView ItemsSource="{Binding Robots}" DisplayMemberPath="name" />
The Robot property should be named Name
- using Pascal Casing to adhere to widely accepted naming conventions:
public class Robot
{
public string Name { get; }
public Robot(string name)
{
Name = name;
}
}
The XAML would then be
<ListView ItemsSource="{Binding Robots}" DisplayMemberPath="Name" />