DisplayMemberPath databinding

75 views Asked by At

I have a puzzled problem of databinding in WPF. There is a listbox in XAML which it has linked with ItemSource, but when it runs, it shows the lists of class names. so I have applied to DisplayMemberPath, but it doesn't helpful. and also I'm wondering how I can access inside class from generic class. Thanks.

result

puzzled.Member
puzzled.Member
puzzled.Member
puzzled.Member

<DockPanel>        
    <ListBox Name="lbxMbrList" DockPanel.Dock="Left" Width="200" Padding="10"></ListBox>
    <ContentControl />
</DockPanel>

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    members.Add(new Member("superman", "123-1234567", "address1"));
    members.Add(new Member("batman", "111-111111", "address2"));
    members.Add(new Member("goodman", "222-222222", "address3"));
    members.Add(new Member("badman", "333-333333", "address4"));

    lbxMbrList.ItemsSource = members;
    lbxMbrList.DisplayMemberPath = members.MemberDetails;   //<<it won't helpful

    //var i = members.member.Name;                          //<<how can I access inside class?
    //if (i == "superman")
    //{
    //    MessageBox.Show("superman");
    //}

}

public class Member
{
    private string _name;
    private string _phone;
    private string _address;

    public string Name { get { return _name; } set { _name = value; } }
    public string Phone { get { return _phone; } set { _phone = value; } }
    public string Address { get { return _address; } set { _address = value; } }

    public Member() { }

    public Member(string name, string phone, string address)
    {
        _name = name; _phone = phone; _address = address;
    }

    public string lbxMember
    {
        get { return string.Format("{0} - {1}", Name, Phone, Address); }
    }

}

class MemberList : IEnumerable<Member>
{
    private ObservableCollection<Member> memberList = new ObservableCollection<Member>();

    public Member this[int i]
    {
        get {return memberList[i];}
        set {memberList[i] = value;}
    }

    public void Add(Member member)
    {
        memberList.Add(member);
    }

    public void Remove(Member member)
    {
        memberList.Remove(member);
    }

    public IEnumerator<Member> GetEnumerator()
    {
        return memberList.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } 



    public Member member { get; set; }     //<< it think I has misunderstood it
    public string MemberDetails
    {
        get
        { return string.Format("{0} - {1}", member.Name, member.Phone, member.Address); }
    }

}  
2

There are 2 answers

4
paparazzo On BEST ANSWER
lbxMbrList.DisplayMemberPath = "lbxMember";

or

lbxMbrList.DisplayMemberPath = "MemberDetails";

And MemberDetails need to be a property of Member (not MemberList)

4
Walt Ritscher On

You are assigning the output of your MemberDetails property to the DisplayMemberPath. Instead, you need to assign the name of the property as a string.

lbxMbrList.DisplayMemberPath = "MemberDetails";

For what its worth, this will be easier to work with if you use an ItemTemplate in the ListBox.

[Edit]

Also, as @Blam mentions in his answer, your MemberDetails property is defined in the wrong class, it needs be in the Member class.