Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DisplayMemberPath is not working in WPF

Tags:

wpf

listbox

I want to display CustomerList\CustomerName property items to the ListBox using ItemsSource DisplayMemberPath property only. But it is not working. I do not want to use DataContext or any other binding in my problem. Please help. My code is given below:

MainWindow.xaml.cs

namespace BindingAnItemControlToAList
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class Customer
    {
        public string Name {get;set;}
        public string LastName { get; set; }
    }
    public class CustomerList 
    { 
        public List<Customer> Customers { get; set; }
        public List<string> CustomerName { get; set; }
        public List<string> CustomerLastName { get; set; }
        public CustomerList() 
        { 
            Customers = new List<Customer>();
            CustomerName = new List<string>();
            CustomerLastName = new List<string>();
            CustomerName.Add("Name1");
            CustomerLastName.Add("LastName1");
            CustomerName.Add("Name2");
            CustomerLastName.Add("LastName2");

            Customers.Add(new Customer() { Name = CustomerName[0], LastName = CustomerLastName[0] });
            Customers.Add(new Customer() { Name = CustomerName[1], LastName = CustomerLastName[1] });
        } 
    } 
}

**MainWindow.Xaml**
<Window x:Class="BindingAnItemControlToAList.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:BindingAnItemControlToAList"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" >
    <Window.Resources>
        <local:CustomerList x:Key="Cust"/>
    </Window.Resources>
    <Grid Name="Grid1">
        <ListBox ItemsSource="{Binding Source={StaticResource Cust}}"  DisplayMemberPath="CustomerName" Height="172" HorizontalAlignment="Left" Margin="27,23,0,0" Name="lstStates" VerticalAlignment="Top" Width="245" />
           </Grid>
</Window>
like image 942
WpfBee Avatar asked Oct 23 '25 03:10

WpfBee


2 Answers

Your're not setting a "collection" as the ItemsSource...thus you don't get anything to choose from. This will choose the list CustomerName.

<ListBox ItemsSource="{Binding Source={StaticResource Cust}, Path=CustomerName}" 
    Height="172" HorizontalAlignment="Left" Margin="27,23,0,0" Name="lstStates" 
    VerticalAlignment="Top" Width="245" />

But really, you should restructure your CustomerList class...there's no need to have separate lists that copy the Name and LastName fields of the Customer - it means you are duplicating data unnecessarily, and also it's possible for the data to get out of sync with each collection.

You also should consider using INotifyPropertyChanged, and use INotifyCollectionChanged on your classes (e.g. use ObservableCollection instead of List, and implement INotifyPropertyChanged on the Customer class) if it's possible to change the collection or data.

e.g.

public class CustomerList
{
    public ObservableCollection<Customer> Customers { get; set; }
    public CustomerList()
    {
        Customers = new ObservableCollection<Customer>();
        Customers.Add(new Customer { Name = "Name1", LastName = "LastName1" });
        Customers.Add(new Customer { Name = "Name2", LastName = "LastName2" });
    }
}

<ListBox ItemsSource="{Binding Source={StaticResource Cust}, Path=Customers}" DisplayMemberPath="Name" Height="172" HorizontalAlignment="Left" Margin="27,23,0,0" Name="lstStates" VerticalAlignment="Top" Width="245" />
like image 99
CSmith Avatar answered Oct 26 '25 02:10

CSmith


There are a couple of things off here. First, your source is set to the object, but the actual list is a property Customers of the object. So you need to use Source={StaticResource Cust},Path=Customers}. Second, the DisplayMemberPath needs to be a property of the item, which in this case is Customer -- so you have to use Name instead of "CustomerName". This works:

<ListBox 
    ItemsSource="{Binding Source={StaticResource Cust},Path=Customers}"  
    DisplayMemberPath="Name" />
like image 25
McGarnagle Avatar answered Oct 26 '25 02:10

McGarnagle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!