Binding to Object properties in WPF

posted in: Uncategorized | 0

BronwenWeeGo.jpgBeen experimenting with WPF again.  Wanted to bind a listview to properties of an object.  I wanted to list the meals and types of meals that i’d selected for today.  So I had a List of Meals.

So first I I bound my listview’s itemsource to my list of meals.  This displayed the following:

DefaultBinding.jpg

Which is a bit useless.  My Meal object has a few  properies i want to display in the list…the MealType and the MealName.  To do this in WPF I had to create a data template in the page resources like:

 
    <Page.Resources>
        <DataTemplate x:Key="selectedMealsTemplate">
            <StackPanel>
                <TextBlock Text="{Binding Path=MealType}"/>
                <TextBlock Text="{Binding Path=MealName}"/>
            </StackPanel>
        </DataTemplate>
    </Page.Resources>

and then set the item template property of my list in the xaml to the name of the template e.g.

ItemTemplate="{ StaticResource selectedMealsTemplate}"

Now when i run it looks like this:

DataTemplate.jpg

Now to work out how to make it look pretty


kick it on DotNetKicks.com