![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | 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 |
| | #2 (permalink) |
| Guest | 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/ |
| | #3 (permalink) |
| Guest | 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 |
| | #5 (permalink) |
| Guest | 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 |
| |
| |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to bind to AD without displaying the Distinguished Name | John | PowerShell | 11 | 04-18-2008 09:02 AM |
| in a bind | tony | Vista mail | 1 | 09-07-2007 09:07 PM |
| Bind to LDAP Directory | Tom G. | PowerShell | 11 | 12-27-2006 12:59 PM |
| Getting ListView items to exand to width of ListView | John Dunn | Avalon | 0 | 11-29-2006 12:45 PM |
| How to bind to a DataRelation in a DataSet | jmagaram | Avalon | 3 | 11-07-2006 01:53 PM |