ListBox.DisplayMember = [String] Can I somehow handle it differently than a string in .NET?

A question has been asked regarding a DataSource. So consider that I have a class

public class Customer{

    public String name;
    public int age;

    public Customer(String name, int age) {
         this.name = name;
         this.age = age;
    }
}

And I have a list binding to a list of these objects. Therefore i say

listBox.DisplayMember = "name";

But my question is when I reorganize my Customer class name into

public String fullName;

DisplayMember still remains in the "name". It will not succeed. Thus, it reduces my ability to reorganize domain objects. Is there any way to do this?

+3
source share
6 answers

The only way I found this is to use additional properties for objects, so

string DisplayMember
{
    get { return 'name'; }
}

, , , .

, , hardcoding !

Oneshot

+1

...

, :

textBoxCustomerName.DataBindings.Add("Text", bindingSource, "CustomerName");

- - :

dataSource.CreateBinding(textBoxCustomerName, ctl => ctl.Text, data => data.Name);

, (, Name CompanyName).

?? // " >

.

+5

DisplyMember , ListBox ToString() . , . , , ToString().

+1

, , . , . , , , , , - .

0

ReSharper ... - . , CodeRush . .

0
source

The only valid way that I found to get around this “problem” would be to add a new public ReadOnly property. Name this property "ListDisplay" or something similar. Then you can change the implementation without any impact on the user interface.

This is not an ideal situation, but it works.

0
source

Source: https://habr.com/ru/post/1710486/


All Articles