简体   繁体   中英

C# WPF, ListView and GridView, dynamic columns, and issues with data binding

I have a custom class:

public class myField {
    public string MyName;
    public string MyDataType;
    public Field details;
}

The custom class "Field" is defined from an imported Salesforce.com Partner WSDL. It looks something like this:

public class Field {
    public string name;
    public string label;
    public string type;
    public int length;
    public bool custom;
    ... // there are probably 50+ defined properties here
}

I have created a list of myField objects, to hold a list of these fields:

public List<myField> MasterList = new List<myField>();

This list is populated through some API calls, I have validated that the data is there and is not null.

My goal is to show the fields "MyName", "MyDataType", as well as all of the sub-properties under the "details" field, in a ListView.

public GridView myGridView = new GridView();
GridViewColumn theColumn;

theColumn = new GridViewColumn();
theColumn.Header = "MyName";
theColumn.DisplayMemberBinding = new Binding("MyName");
myGridView.Columns.Add(theColumn);

theColumn = new GridViewColumn();
theColumn.Header = "length";
theColumn.DisplayMemberBinding = new Binding("details.length");
myGridView.Columns.Add(theColumn);

myListView.View = myGridView;
myListView.ItemsSource = MasterList;

The problem that I have, is that any Main Properties that I try to display work just fine (MyName, MyDataType), but any sub-properties (sub-fields that I pull through the "details" field) do NOT display at all (name, label, type, length, custom, etc).

binding = "MyName" // displays correctly
binding = "MyDataType" // displays correctly
binding = "details.name" // does not display correctly
binding = "details.label" // does not display correctly
etc

Does anyone know how to solve this?

I copied your code and even the MyName and MyDataType failed to display, unless I change them to below

public class myField {
    public string MyName {get; set;}
    public string MyDataType {get; set;}
    public Field details {get; set;}
}

public class Field {
    public string name {get; set;}
    public string label {get; set;}
    public string type {get; set;}
    public int length {get; set;}
    public bool custom {get; set;}
}

Then everything works just fine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM