0

My question is for WPF (not win forms) and is very similar to this: Add Items to Columns in a WPF ListView

However, I have legacy problems as I have to use a struct which contains about a 100 variables. The struct is required to be passed into a function which will update its values.

struct alotofthings
{
float item1;
float item2;
.
.
.
UInt16 item100;
}

I use System.Reflections to extract out the fieldinfo.name and GetValue() to get the values of the variables. Next I want to display them in a ListView (created at runtime) like

Variable name | Value (<--- This are the column headers)

item1 .............. | 1223.3

item2 ..... ........ | 6.673

... .....................| ...

item100 ...........| 1

The values will dynamically change every few milliseconds as the struct object is updated using a system pipe subscription. I thought of clearing the ListView and repopulating the graph every time this happens (Method 1). But I need help with adding the items to their respective columns.

I also thought of creating a class with INotifyPropertyChanged (Method 2), but this will become the same situation as method 1 as I still have to update the class using the updated struct. I also do not know how to set the binding dynamically as my class would have 100 properties.

Finally I did thought of using ObservableCollection, but then I also will have to clear its contents and re-add items again as the struct gets updated via the legacy updating function (Method 3). I don't know how fast this is compared to method 1.

Community
  • 1
  • 1
Mittens
  • 35
  • 1
  • 11

1 Answers1

1

Try This Method

      <ListView x:Name="lstvItemsList">
      <ListView.View>
         <GridView>
            <GridViewColumn Header="Id" DisplayMemberBinding="{Binding itemName}"/>
             <GridViewColumn Header="Name" DisplayMemberBinding="{Binding value}"/>
         </GridView>
      </ListView.View>
     </ListView>

Then create a structure for hold values

    public struct Items
    {
        public string itemName { get; set; }
        public string value { get; set; }
    }

Then bind values like this

       lstvItemsList.Items.Add(new Items
                            {
                                itemName ="item1" ,
                                value = "125"
                            }
Shahid Neermunda
  • 1,317
  • 1
  • 14
  • 28
  • Doh.. thanks I got it. I will use a foreach to encapsulate the lstvItemsList.Items.Add() function and populate the Listview with my 100 variables this way. If this works without lagging at runtime then I'll accept the answer in a couple of days :) Although I am worried as my values aren't strings but a variety of float and integers. – Mittens Oct 14 '15 at 06:44
  • @Mittens if you can go with this, then what about the struct of about 100 properties? If that's what you have then you cannot go with this solution. You have to re-design much. And every time you want to change a property on the struct, instead of writing this `s.SomeProperty = something;`, you have to write this `var item = items.FirstOrDefault(p => p.itemName == "SomeProperty"); if(item != null) item.value = something;`. – King King Oct 14 '15 at 06:44
  • this is considered as a work-around rather directly solving the OP's problem. The original problem does make sense and it involves the so-called ***pivoting***. Of course directly solving that problem is not easy. – King King Oct 14 '15 at 06:48
  • I am sorry maybe I wasn't clear about the struct. I have to declare the struct as:'alotofthings stucture1' and pass it to a function to get data: 'GetData( ref structure1)'. Then I will probably try the above method and convert the fieldname and values of the struct variables to string e,g [Items.value = structure1.item1.ToString]. Well gonna try this first. – Mittens Oct 14 '15 at 06:52
  • @King King O.o Sorry I am just a beginner programmer. I don't even understand what you meant by pivoting! Maybe you can try creating question on it if it has been unasked. You will probably be much clearer than me... Also, I do think there might be a better solution to my question but I am gonna try this first :3 – Mittens Oct 14 '15 at 07:00