Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

How to Bind ListView to List<MyStruct>

Closed Thread
 
Thread Tools Display Modes
Old 03-14-2006   #1 (permalink)
Sam Jost
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

Old 03-14-2006   #2 (permalink)
Drew Marsh
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/


Old 03-15-2006   #3 (permalink)
Sam Jost
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

Old 03-15-2006   #4 (permalink)
Sam Jost
Guest


 

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

Old 03-15-2006   #5 (permalink)
Sam Jost
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


Closed Thread

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








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50