Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, guys
I've managed to do Data Binding in Xaml but not completely.
I have got text block and Data Binding works. I'm passing information from page 2 to page3 in my windows app.
I have done exactly the same with my List View and it doesn't work at all. I don't understand why. Can someone please have a look?

XML
<ListView Name="detailsListView" Grid.Row="1" Height="300" Width="400" 
ItemsSource="{Binding Name}"/>

        <TextBlock Text="{Binding Name}" x:Name="myTextBlock" Grid.Row="1" Grid.Column="2" Height="200" Width="300" FontSize="28"/>


In my Code Behind The data context is set to the page layout

C#
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    Nutrient nutriQuery = e.Parameter as Nutrient;
    if (nutriQuery != null)
    {
        DetailsPageLayout.DataContext = nutriQuery;
    }


Why Text Block works but not List View?
Posted
Comments
dan!sh 9-Mar-15 3:53am    
You will need to set the template for the listview.
Reatellino 9-Mar-15 6:42am    
listview
ListView.itemtemplate
dataTemplate
StackPanel
TextBlock text={binding Name} :(

1 solution

You need to bind the ItemsSource to an ObservableCollection. You also need to define the ListView View. Something like this.


C#
<!--In the view model, PersonCollection is a collection of type Person
SelectedPerson is a property of type Person.Name and Age are  properties defined     inside the Person class-->
           <listview itemssource="{Binding PersonCollection}" selecteditem="{Binding SelectedPerson, Mode=TwoWay}" issynchronizedwithcurrentitem="True">
               HorizontalAlignment="Center" Height="168"  VerticalAlignment="Top" Width="400" Background="#FFBFBFBF">
               <listview.view>
                   <gridview>
                       <gridviewcolumn width="280" header="Name" displaymemberbinding="{Binding Path=Name}" />
                       <gridviewcolumn width="60" header="Age" displaymemberbinding="{Binding Path=Age}" />
                   </gridview>
               </listview.view>
           </listview>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900