ListView with programmatically added columns shows item type

I got ListView in another assembly and want to add a column to it. Elements in a list view are class objects in this assembly.

When I add a column like this

var col : GridViewColumn = new GridViewColumn(); col.Header = "Header text"; list.View.Columns.Add(col); 

a column appears, but all the rows in the list have the item type name in this new column. Even when I change the value of a new column ( itemclass.Items[newColIdx] = "zz" ), it does not appear. No data binding or item templates.

Why is this?

Change Perhaps I can reinforce the question: if ListView.Items contains an array of class A - how do you control what things A are displayed, like in a GridView? I need reverse engineering here, and DisplayMemberBinding is null, so it does not use AFAIK data binding.

Question about Bounty . When ToString() is called for each element (representing a row), how is it displayed in the columns of the GridView? How do I change an element in a row so that the new column is filled?

A particular problem when trying to find this solution is that it runs in the JScript sandbox in a larger C # .NET application, and no debugging options are provided except for using message boxes and reflection.

Change The problem is that I am writing JScript that works in a third-party .NET application and gives me the opportunity to do a lot with .NET. This is a script to add functions to a list. I can access the list and populate the database data with a third-party application. I don’t know how it turns out. I can programmatically add custom rows with custom content (not database related). But when I add my own column, I seem to be unable to determine its value in the den rows.

The added rows are of type 3rdParty.ListRow and contain the Items property, to which I assign an array of strings. Array values ​​are displayed in representative columns, but not in added columns. There I only get the text 3rdParty.ListRow , because ToString() was called on ListRow.

So: How can I specify new columns (for example, index 5) to display ListRow.Items[5] rather than ListRow.ToString() ? I liked doing this the same way as in existing columns, it seems that all Alex properties are mentioned undefined. How would I set DataMemberBinding to achieve a mapping to ListRow.Items[5] ?

+4
source share
2 answers

If the column does not have DisplayMemberBinding or CellTemplate , then ToString is called for each element in the grid, and the evaluated value is used as the contents of the cell.

For example, if you have a class:

 public class Person { public string FirstName { get; set; } public string LastName { get; set; } public override string ToString() { return LastName; } } 

If you bind the Person collection to a grid view:

 var people = new List<Person> { new Person { FirstName = "Peter", LastName = "Smith" }, new Person { FirstName = "Steve", LastName = "White" }, new Person { FirstName = "Dave", LastName = "Gates" }, }; listView1.ItemsSource = people; 

And if you create a new column without binding, LastName will be shown in this column, because the overridden ToString() returns LastName . If you modify ToString to return FirstName , then FirstName appears in the new column.

I hope I understood your question correctly.

[change]

If you add many new columns and do not use DisplayMemberBinding , then all new columns will evaluate and display the result of ToString() . You need to set DisplayMemberBinding to display a specific property.

 var newColumn = new GridViewColumn(); newColumn.Header = "Test"; newColumn.DisplayMemberBinding = new Binding("FirstName"); gridView.Columns.Add(newColumn); 

[edit] Answers to your questions:

When ToString () is called on each element (which represents a row), how are those displayed on the View grid columns? How to change an element in a row so that the new column is filled?

ToString is called when the cell's presenter is displayed, unless you specify a binding or template that will also use a binding at the end. You cannot modify a row element to control how it appears in the grid unless you override ToString or column-bound properties.

Not sure if I fully understand your problem. You are adding a new column, right? Why don't you set the DisplayMemberBinding property in your column? If you do this, you can display whatever you want in the column.

[change]

I think I answered your question about generosity :). I think I have already earned generosity :).

However, here is the answer to your next question:

How would I set DataMemberBinding to achieve a mapping to ListRow.Items [5]?

Answer:

 newColumn.DisplayMemberBinding = new Binding("Items[5]"); 
+3
source

This is because the ToString () method is called for each item in the list. If not overridden by the inheriting type, Object.ToString () returns the type name of the object.

Another problem is when your list will not generate change events, so the user interface does not know that the data has changed and needs to be updated.

+3
source

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


All Articles