views:

39

answers:

1

I don't know how to bind a List of Drink to a WPF TreeView.

struct Drink
{
    public string Name { get; private set; }
    public int Popularity { get; private set; }

    public Drink ( string name, int popularity )
        : this ( )
    {
        this.Name = name;
        this.Popularity = popularity;
    }
}

List<Drink> coldDrinks = new List<Drink> ( ){
    new Drink ( "Water", 1 ),
    new Drink ( "Fanta", 2 ),
    new Drink ( "Sprite", 3 ),
    new Drink ( "Coke", 4 ),
    new Drink ( "Milk", 5 ) };
        }
    }

I have searched the web, for instance saw here. But what's this even mean: ItemsSource="{x:Static local:TreeTest.BoatList}"

x:? static? local?

How do you specify a collection in your code in the xaml?

+2  A: 

Typically, you'll want to make sure to set the DataContext for UserControl or Window, and then have your "coldDrinks" be a collection defined as a property in your DataContext class.

If you do this, then all that's required would be:

<TreeView ItemsSource="{Binding ColdDrinks}" />

I strongly recommend reading up on WPF's Data Binding model. Understanding this fully is one key to making WPF really enjoyable.


On a side note: given your recent questions, and the approach you seem to be taking, you may want to consider taking a bit of time to read up on MVVM, and other architectural approaches. Even if you don't use MVVM, the articles describing it may help you "think" in WPF terminology, which will help tremendously.

I wrote a whole series on migrating from Windows Forms style programming to WPF. It discusses specific WPF features, and why you should change the way you think, but introduces some of the concepts a bit more slowly than most pure "MVVM" articles. Figured I'd mention it in case it helps.

Reed Copsey
Thanks, how do you do the first thing you mentioned? "Typically, you'll want to make sure...". Do I have to do it?
Joan Venge
My coldDrinks is inside Window1 as an instance value. When I ran it, it shows no items at all. Does it have to be static?
Joan Venge
@Joan: You can just do "this.DataContext = this;" in your constructor, in code behind, if you want. However, I strongly recommend reading the articles I posted in my update - it'll help a ton to understand WHY you want to do some of these things, instead of just trying to fumble through making things work...
Reed Copsey
@Joan: No. It doesn't need to be static. It DOES need to be a property, and not a field. Also, unless you use ObservableCollection<T> instead of List<T>, you'll need to make sure the items exist before the Window is loaded, or the binding won't work properly...
Reed Copsey
Thanks I will give them a read. Probably only your last link because I don't wanna read 100s of pages before i could start doing anything or I will just get cold towards wpf.
Joan Venge
@Joan: The last link is one I wrote - It's really an intro to WPF, and MVVM, for Windows Forms devs. I think it might really help :)
Reed Copsey
So you mean I can't have something like this?public List<Drink> list = new List<Drink> ( );It has to be real property?
Joan Venge
Xaml isn't so hard it seems, you are correct, but some of the concepts I have no idea what's going on.
Joan Venge
@Joan: Yes. Databinding only works against properties in WPF. And I agree - XAML is pretty easy, but understanding why you'd want to use certain things, and when/how to use specific features, is tough. Understanding some of the design patterns like MVVM really helps here.
Reed Copsey
But is MVVM really hard to learn? If I can't convert some of my tools from widows forms to wpf in a short time, then it will be hard to convert them at all.
Joan Venge
@Joan: No. It's not hard to learn. Even if you don't use it, it's worth understanding why so many people do. MVVM is easy, once you understand a couple of WPF fundamentals. I'd say read my articles, and if you're still having questions, post comments there (on specific posts) and I'll be happy to respond. Also, I'm sure it'll trigger other good SO questions ;)
Reed Copsey
@Joan: I suspect you'll save more time, even in the short term, by understanding WHY to use the techniques in WPF... MVVM helps with that.
Reed Copsey
Thanks Reed. I will read your stuff this weekend and let you know if I don't understand something there :O
Joan Venge