ListViewItems无法正确显示

时间:2013-02-19 23:02:01

标签: c# xaml windows-8 windows-runtime winrt-xaml

我的应用中有ListView,应该显示从服务器加载的数据。

hovering over item http://s12.postimage.org/ul5yfk131/image1.png selected item http://s17.postimage.org/v0eak9h8v/image2.png

ListView有两个ItemTemplates(带有和Image控件,TextBlocks,Grid,Stackpanel),它们是用模板选择器选择的。数据本身已正确加载,但控件不会在应用程序的第一次启动时显示,但并非总是如此,有时它们显示正确,有时不显示。当我滚动列表视图时项目不可见时,项目将正确显示。我的问题是什么可以导致这种行为?

编辑: 好的,这是我的代码

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"/>

我编辑了listview控件,因为我遇到了像this one这样的问题。如果我使用普通ListView或使用默认ItemContainerStyle,则不会更改。

现在这里是单独文件中的模板,如果Item的EventListHeaderTemplate属性为true,则选择isTournament(这是我对项目进行分组的方式)。这是一种原始方式,我将尝试使用CollectionViewSource对项目进行分组,看看是否可能是这个问题。

<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>

首先我教过图像是问题所在,但是当我从模板中删除图像时问题仍然存在。

和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;
    }

除了SelectionChanged处理程序之外,我在listview后面的代码中没有任何内容,我认为这与该问题无关。

0 个答案:

没有答案