Click here to Skip to main content
15,747,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to bind data inside a listview that contains textblock , the problem is that I can only show the first element of the model ( the first row of the listview ) , I would like to bind all data in the listview.

What I have tried:

<ListView x:Name="listviewgrd" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >

                          <Grid HorizontalAlignment="Right" MinWidth="606" MinHeight="10">
                                  <ColumnDefinition Width="300*"/>
                                  <ColumnDefinition Width="100*"/>
                                  <ColumnDefinition Width="100*"/>
                                  <ColumnDefinition Width="100*"/>

                          <TextBlock x:Name="servicename" Text="{Binding Name ,Mode=TwoWay, NotifyOnTargetUpdated=True}" Grid.Column="3" FlowDirection="RightToLeft" Grid.ColumnSpan="1" />
                          <TextBlock Text="{Binding Price,Mode=TwoWay, NotifyOnTargetUpdated=True}" Grid.Column="2" HorizontalAlignment="Center" FlowDirection="RightToLeft"  Grid.ColumnSpan="1" />
                          <TextBlock Text="{Binding Status,Mode=TwoWay, NotifyOnTargetUpdated=True}" Grid.Column="1" HorizontalAlignment="Center" FlowDirection="RightToLeft" Grid.ColumnSpan="1" />
                          <TextBlock Text="{Binding Parent Name,Mode=TwoWay, NotifyOnTargetUpdated=True}" Grid.Column="0" HorizontalAlignment="Center" Foreground="#FF62013C"  FlowDirection="RightToLeft"  Grid.ColumnSpan="3" />

                          <Style TargetType="ListViewItem">
                              <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                              <Setter Property="VerticalContentAlignment" Value="Stretch"/>

listviewgrd.DataContext = person.Status;

Updated 15-Sep-20 5:04am

Are you saying that person.Status is an IObservableCollection<> and your ListView is only showing one row?

If so, it's probably because you're not leveraging the ItemTemplate property correctly, which is used to indicate how each item is displayed:
<ListView ..>

In the example you've provided you're effectively overriding what the content of the ListView contains, which is why you're only seeing it as one row.
Share this answer
Not sure how much you know about MVVM. Read/Learn about it:
Model-View-ViewModel (MVVM) Explained[^]
MVVM for Beginners[^]

With MVVM, you would tie your listview with a data model that is of collection type. That collection would have some property at item level that can be used to bind your textblock.

Sample: ListView, data binding and ItemTemplate - The complete WPF tutorial[^]

Try out.
Share this answer
Thanks to all, I have changed to listView with GridView and it work for now
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