The following forum(s) are migrating to a new home on Microsoft Q&A (Preview): Developing Universal Windows apps!

Ask new questions on Microsoft Q&A (Preview).
Interact with existing posts until December 13, 2019, after which content will be closed to all new and existing posts.

Learn More

 none
[WP8.1] [C#] listview DataContext not shown all the items RRS feed

  • Question

  • Hi to all,

    I have a listview and I binding the items by a custom class.

    my problem is that sometimes the first item in the listview, is not showing allways. When I cannot see it, I refresh the DataContext and then everything is as suppose to be.

    what I am not doing well?

    thanks in advance


    Friday, July 17, 2015 5:48 PM

Answers

  • When you bind an itemssource then each of the items' datacontext is the item out that list they are presented with.

    You've got some weird stuff there.

    You should lose that datacontext you have on the grid.

    <ListView x:Name="GameLst" ItemsSource="{Binding}" Margin="10" Grid.Row="1" Tapped="GameLst_Tapped" >
     <ListView.ItemTemplate >
       <DataTemplate >
    	<Border Background="#FFD8E1C8" BorderThickness="5" CornerRadius="10,10,10,10"  >
            <Grid  Width="{Binding ElementName=GameLst, Path=ActualWidth}" >
    ...

    Also, the border isn't in a grid so remove the Grid.Row="0" Grid.Column="0" you have on it.

    Rather than binding the width of that grid like you have there I would recommend you apply a style to the itemcontainerstyle like:

    <ListView.ItemContainerStyle>
       <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
       </Style>
    </ListView.ItemContainerStyle>

    You might find my TechNet article interesting:

    http://social.technet.microsoft.com/wiki/contents/articles/31410.uwa-simple-listviewitem-styling.aspx

    There's a sample you can download and look at.


    Saturday, July 18, 2015 2:17 PM

All replies

  • How are you setting the itemssource? 
    Friday, July 17, 2015 6:02 PM
  • Hello Vagelis,

    Please read Guide to posting: subject line tags

    Can you share some code with us please?

    George
    Friday, July 17, 2015 7:50 PM
  • my xaml code:

    <ListView x:Name="GameLst" ItemsSource="{Binding}" Margin="10" Grid.Row="1" Tapped="GameLst_Tapped" >
    
    	<ListView.ItemTemplate >
    		<DataTemplate >
    			<Border Background="#FFD8E1C8" Grid.Row="0" Grid.Column="0" BorderThickness="5" CornerRadius="10,10,10,10"  >
    
    				<Grid DataContext="{Binding}" Width="{Binding ElementName=GameLst, Path=ActualWidth}" >
    ...

    my c# code:

    for (int i = 0; i < games.Count(); i++)
    {                    
    	myGameClass.Add(new GameClass
    	{
    		GameId = Convert.ToInt32(games[i].properties.First(a => a.id == 6).Value),
    		...
    	}
    }
    
    GameLst.DataContext = myBetClass;

    Saturday, July 18, 2015 8:35 AM
  • Can you try if that works for you? Listview first item not shown
    Saturday, July 18, 2015 11:14 AM
  • in my app I have it in the "OnNavigatedTo" event
            protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                if (e.NavigationMode != NavigationMode.Back)
                {
                    //για να μην ξαναψαχνει και να παει εκει που το αφησαμε
                    Exec();
                }
            }

    but it is not always, but randomly happens
    Saturday, July 18, 2015 12:24 PM
  • When you bind an itemssource then each of the items' datacontext is the item out that list they are presented with.

    You've got some weird stuff there.

    You should lose that datacontext you have on the grid.

    <ListView x:Name="GameLst" ItemsSource="{Binding}" Margin="10" Grid.Row="1" Tapped="GameLst_Tapped" >
     <ListView.ItemTemplate >
       <DataTemplate >
    	<Border Background="#FFD8E1C8" BorderThickness="5" CornerRadius="10,10,10,10"  >
            <Grid  Width="{Binding ElementName=GameLst, Path=ActualWidth}" >
    ...

    Also, the border isn't in a grid so remove the Grid.Row="0" Grid.Column="0" you have on it.

    Rather than binding the width of that grid like you have there I would recommend you apply a style to the itemcontainerstyle like:

    <ListView.ItemContainerStyle>
       <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
       </Style>
    </ListView.ItemContainerStyle>

    You might find my TechNet article interesting:

    http://social.technet.microsoft.com/wiki/contents/articles/31410.uwa-simple-listviewitem-styling.aspx

    There's a sample you can download and look at.


    Saturday, July 18, 2015 2:17 PM
  • now works just fine

    thanks a lot

    Saturday, July 18, 2015 5:26 PM