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 y开发者_C百科our code in the xaml?
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.
精彩评论