![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
| |
| | #1 (permalink) |
| | How to Bind ListView to List<MyStruct> I want to show some data in a List<> in a ListView, but don't know how to bind this. my data (.xaml.cs): ----- public struct MyStruct { public MyStruct(String name, int size) { Name = name; Size = size; } public String Name; public int Size; } [in OnInitialized] List<MyStruct> mydata= new List<MyStruct>(); mydata.Add(new MyStruct("Foot", 23)); mydata.Add(new MyStruct("Whatever", 4711)); MyListview.DataContext = mydata; ------ my xaml (.xaml): ------ <ListView x:Name="MyListview" ItemsSource="{Binding}"> <ListView.View> <GridView> <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=Name}"/> <GridViewColumn Header="Size" DisplayMemberBinding="{Binding Path=Size}"/> </GridView> </ListView.View> </ListView> ----- The ListView displays to empty rows. The header do look good, 's just the content of the cell is missing, so I'd guess my binding is wrong - pity is I got no clue how this should look. ![]() Can anyone help? Sam |
My System Specs![]() |
| | #2 (permalink) |
| | Re: How to Bind ListView to List<MyStruct> Sam Jost wrote: > I want to show some data in a List<> in a ListView, but don't know how > to bind this. > > ... code snipped for brevity ... > > The ListView displays to empty rows. The header do look good, 's just > the content of the cell is missing, so I'd guess my binding is wrong - > pity is I got no clue how this should look. ![]() > > Can anyone help? Sam, Everything looks great except for one thing: you need to make the public fields properties instead. Databinding, even in ASP.NET and Windows Forms (IIRC) does not work against fields, only against properties. So if you just make your fields private and add property accessors for them, all of your other code should just work. HTH, Drew ___________________________________ Drew Marsh Chief Software Architect Mimeo.com, Inc. - http://www.mimeo.com Microsoft C# / WPF MVP Weblog - http://blog.hackedbrain.com/ |
My System Specs![]() |
| | #3 (permalink) |
| | Re: How to Bind ListView to List<MyStruct> Drew Marsh schrieb: > Sam Jost wrote: > > > I want to show some data in a List<> in a ListView, but don't know how > > to bind this. > > > > ... code snipped for brevity ... > > > > The ListView displays to empty rows. The header do look good, 's just > > the content of the cell is missing, so I'd guess my binding is wrong - > > pity is I got no clue how this should look. ![]() > > > > Can anyone help? > > Sam, > > Everything looks great except for one thing: you need to make the public > fields properties instead. Databinding, even in ASP.NET and Windows Forms > (IIRC) does not work against fields, only against properties. So if you just > make your fields private and add property accessors for them, all of your > other code should just work. > > HTH, > Drew Drew, aaarrg, silly me - I thought to make a working sample as simple as possible, and therefore tried the short way to use fields. Properties work like charm, thanks a lot! Sam |
My System Specs![]() |
| | #4 (permalink) |
| | Re: How to Bind ListView to List<MyStruct> Another (related) question: How do I refresh the ListView when one of my structs has changed? Sam |
My System Specs![]() |
| | #5 (permalink) |
| | Re: How to Bind ListView to List<MyStruct> Got it! Just had to add the INotifyPropertyChanged interface to my custom struct, then it all updated nicely. Sam Sam Jost schrieb: > Another (related) question: > > How do I refresh the ListView when one of my structs has changed? > > Sam |
My System Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| in a bind | Vista mail | |||
| Bind to LDAP Directory | PowerShell | |||