0

我的应用程序中有一个ListView应该显示从服务器加载的数据。

悬停在项目上 所选项目

ListView 有两个 ItemTemplates(带有和图像控件、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 控件,因为我遇到了这样的问题ListView如果我使用 normal或使用 default ,它不会改变ItemContainerStyle

现在这里是单独文件中的模板,如果 Item 的属性为 true(这是我对项目进行分组的方式) ,则将EventListHeaderTemplate被选中。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>

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

和模板选择器:

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 处理程序之外,我在此列表视图后面的代码中没有任何内容,我认为这与问题无关。

4

0 回答 0