0

I have a ListView in my app that should display data that is loading from a server.

hovering over item selected item

The ListView has two ItemTemplates (with and Image control, TextBlocks, Grid, Stackpanel) which are selected with a template selector. The data itself is loaded correctly but the controls won't display it on the first start of the app, but its not always the case, sometimes they display correctly and sometimes not. When I scroll the listview where the items were invisible, the items would display correctly. My question here is what can cause this behavior?

EDIT: Ok, here is my code

the ListView:

<customControls:MyListView 
        x:Name="EventListView" 
        ItemsSource="{Binding Events}"
        ItemTemplate="{StaticResource EventListSelectingTemplate}"
        Grid.Column="0" 
        Grid.Row="1"
        SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
        ItemContainerStyle="{StaticResource EventListViewItemDisabledStyleModified}"
        SelectionChanged="EventListView_OnSelectionChanged"/>

I have edited the listview control because I had a problem like this one. It makes no change if I use the normal ListView or if I use the default ItemContainerStyle.

Now here are the templates that are in a seperate file, the EventListHeaderTemplate is being selected if the Item's isTournament property is true (this is my way of grouping the items). This is a primitive way, I will try to group the items with a CollectionViewSource and see if maybe this was the problem.

<DataTemplate x:Key="EventListItemTemplate">
    <Grid Width="{Binding ElementName=EventListView, Path=ActualWidth}" Margin="3">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <StackPanel 
            x:Name="timeAndStatus" 
            Grid.Column="0"
            Orientation="Vertical">
            <TextBlock 
                x:Name="time" 
                Text="{Binding GameStartTime}" 
                Visibility ="{Binding St, Converter={StaticResource StatusToVisibilityConverter}}"
                HorizontalAlignment="Center" />

            <Image 
                x:Name="liveEventImage" 
                Width="16"
                Height="16"
                Source="{StaticResource LiveIcon}"
                Visibility="{Binding ElementName=time, Path=Visibility, Converter={StaticResource VisibilityNegationConverter}}"
                HorizontalAlignment="Center"
                Margin="0,0,0,5"/>

            <TextBlock 
                x:Name="status" 
                Text="{Binding Status}" 
                HorizontalAlignment="Center"/>
        </StackPanel>
        <StackPanel 
            x:Name="teamNames" 
            Grid.Column="1"
            Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <TextBlock 
                    x:Name="homeTeamName" 
                    Text="{Binding Ht}"
                    FontWeight="{Binding Converter={StaticResource EventToFontWeightConverter}, ConverterParameter=HOME}"/>
                <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
                    <Image Source="../Assets/Images/red.png" Visibility="{Binding Ca, Converter={StaticResource NumberOfRedsToImageVisibilityConverter}, ConverterParameter='H1'}"/>
                    <Image Source="../Assets/Images/red.png" Visibility="{Binding Ca, Converter={StaticResource NumberOfRedsToImageVisibilityConverter}, ConverterParameter='H2'}" Margin="10,0,0,0"/>
                    <Image Source="../Assets/Images/red.png" Visibility="{Binding Ca, Converter={StaticResource NumberOfRedsToImageVisibilityConverter}, ConverterParameter='H3'}" Margin="10,0,0,0"/>
                </StackPanel>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock 
                    x:Name="awayTeamName" 
                    Text="{Binding At}" 
                    FontWeight="{Binding Converter={StaticResource EventToFontWeightConverter}, ConverterParameter=AWAY}"/>
                <StackPanel Orientation="Horizontal" Margin="20,0,0,0">
                    <Image Source="../Assets/Images/red.png" Visibility="{Binding Ca, Converter={StaticResource NumberOfRedsToImageVisibilityConverter}, ConverterParameter='A1'}"/>
                    <Image Source="../Assets/Images/red.png" Visibility="{Binding Ca, Converter={StaticResource NumberOfRedsToImageVisibilityConverter}, ConverterParameter='A2'}" Margin="10,0,0,0"/>
                    <Image Source="../Assets/Images/red.png" Visibility="{Binding Ca, Converter={StaticResource NumberOfRedsToImageVisibilityConverter}, ConverterParameter='A3'}" Margin="10,0,0,0"/>
                </StackPanel>
            </StackPanel>
        </StackPanel>
        <StackPanel 
            x:Name="teamScores" 
            Grid.Column="2"
            Orientation="Vertical">
            <TextBlock 
                x:Name="homeScore" 
                Text="{Binding Hs}" />
            <TextBlock 
                x:Name="awayScore" 
                Text="{Binding As}" />
        </StackPanel>
    </Grid>
</DataTemplate>

<DataTemplate x:Key="EventListHeaderTemplate">
    <Grid 
        Background="Gray" 
        Width="{Binding ElementName=EventListView, Path=ActualWidth}"
        Height="50"
        Margin="0,20,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="4*"/>
        </Grid.ColumnDefinitions>
        <Image 
            x:Name="CountryFlag"
            Grid.Column="0"
            Source="{Binding FlagUrl}"
            Width="40"
            Height="30"/>
        <TextBlock
            Grid.Column="1"
            Text="{Binding TournamentSignature}" 
            FontSize="18" 
            MaxHeight="50"
            VerticalAlignment="Center"
            Padding="0,0,20,0"
            TextWrapping="Wrap"/>
    </Grid>
</DataTemplate>

<DataTemplate x:Key="EventListSelectingTemplate">
    <templateHandling:EventListTemplateSelector Content="{Binding}"
                                EventListItemTemplate="{StaticResource EventListItemTemplate}"
                                EventListHeaderTemplate="{StaticResource EventListHeaderTemplate}"
                                />
</DataTemplate>

First I taught that the Images were the problem, but when I remove the Images from the templates the problem stays.

and the TemplateSelector:

public DataTemplate EventListItemTemplate { get; set; }
    public DataTemplate EventListHeaderTemplate { get; set; }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var ev = item as Event;
        if (ev != null)
        {
            if (ev.IsTournament)
            {
                return EventListHeaderTemplate;
            }
            else
            {
                return EventListItemTemplate;
            }
        }
        return null;
    }

I have nothing in the code behind for this listview besides the SelectionChanged handler, which I think has nothing to do with the problem.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Kristian Vukusic
  • 3,284
  • 6
  • 30
  • 46

0 Answers0