简体   繁体   English

列表框和ItemContainerStyle的错误

[英]Bug with listbox and ItemContainerStyle

I have a simple Listbox 我有一个简单的列表框

<ListBox Name="GenreListBox" ItemsSource="{Binding}" ItemContainerStyle="{StaticResource ListBoxItemGenreStyle}"          SelectionChanged="GenreListBox_SelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Visible">
  <ListBox.ItemTemplate>
     <DataTemplate >
         <Grid Height="115" Background="Transparent"/>
     </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

and ItemContainerStyle(without VisualStates), for example: 和ItemContainerStyle(不带VisualStates),例如:

 <Style x:Key="ListBoxItemGenreStyle" TargetType="ListBoxItem">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                        <Grid x:Name="border">
                            <Image Name="img" Source="{Binding picUrl}" DataContext="{TemplateBinding DataContext}" Width="65" Height="65" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="30,0,0,0" Opacity="0" ImageOpened="Image_ImageOpened"/>
                            <TextBlock Name="title" Text="{Binding title}" DataContext="{TemplateBinding DataContext}" Foreground="Black" FontFamily="Segoe WP SemiLight" FontSize="36" VerticalAlignment="Top" Margin="111,14,0,0"/>
                            <TextBlock Name="artist" Text="{Binding artist}" DataContext="{TemplateBinding DataContext}" Foreground="{StaticResource RedBrush}" FontFamily="Segoe WP" FontSize="21.333" VerticalAlignment="Top" Margin="111,55,0,0"/>
                            <TextBlock Name="price" Text="{Binding price}"  DataContext="{TemplateBinding DataContext}" Foreground="#FF6E6E6E" FontFamily="Segoe WP"   FontSize="20.8" VerticalAlignment="Bottom"  Margin="111,0,0,8" />
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

First 10 items look normal but if I add 10 new items some of them looks like first item, and if I check data in SelectionChanged method data will be normal. 前10个项目看起来很正常,但是如果我添加10个新项目,则其中一些看起来像第一个项目,并且如果我在SelectionChanged方法中检查数据,数据将是正常的。 What is wrong with my code? 我的代码有什么问题?

Here is an example of app that you can build and run If I add 10 items and scroll down you will see that last items look like first, then you shold scroll up and then down then all items look normal. 这是一个可以构建和运行的应用程序示例,如果我添加10个项目并向下滚动,您会看到最后一个项目看起来像第一个,然后按住向上滚动然后向下滚动,则所有项目看起来都正常。

Screenshots 截图

第一第二

First, I'm not sure about this, but I guess you're binding listbox with ObservableCollection Melodies , so in your container style, you're binding Melodies class's variables correctly. 首先,我不确定这一点,但是我想您是用ObservableCollection Melodies绑定listbox ,因此在您的容器样式中,您正确地绑定了Melodies类的变量。 But I didn't get why you're binding DataContext for same variables in custom ItemContainerStyle as you've already binded them ?? 但是我不明白为什么要在自定义ItemContainerStyle为相同的变量绑定DataContext ,因为您已经绑定了它们? {Binding <something>} means Binding {Binding <something>.DataContext} {Binding <something>}表示绑定{Binding <something>.DataContext}

I mean for these tags : 我的意思是这些tags

<Image Name="img" Source="{Binding picUrl}" DataContext="{TemplateBinding DataContext}" Width="65" Height="65" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="30,0,0,0" Opacity="0" ImageOpened="Image_ImageOpened"/>
<TextBlock Name="title" Text="{Binding title}" DataContext="{TemplateBinding DataContext}" Foreground="Black" FontFamily="Segoe WP SemiLight" FontSize="36" VerticalAlignment="Top" Margin="111,14,0,0"/>
<TextBlock Name="artist" Text="{Binding artist}" DataContext="{TemplateBinding DataContext}" Foreground="{StaticResource RedBrush}" FontFamily="Segoe WP" FontSize="21.333" VerticalAlignment="Top" Margin="111,55,0,0"/>
<TextBlock Name="price" Text="{Binding price}"  DataContext="{TemplateBinding DataContext}" Foreground="#FF6E6E6E" FontFamily="Segoe WP"   FontSize="20.8" VerticalAlignment="Bottom"  Margin="111,0,0,8" />

So here you're binding their source / data. 所以在这里,您要绑定他们的源/数据。 But again you're binding its DataContext which in turn is a Data . 但是,您再次binding它的DataContext ,后者又是一个Data This might be causing problem. 这可能引起问题。 So change above to: 因此,将以上内容更改为:

<Image Name="img" Source="{Binding picUrl}" Width="65" Height="65" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="30,0,0,0" Opacity="0" ImageOpened="Image_ImageOpened"/>
<TextBlock Name="title" Text="{Binding title}" Foreground="Black" FontFamily="Segoe WP SemiLight" FontSize="36" VerticalAlignment="Top" Margin="111,14,0,0"/>
<TextBlock Name="artist" Text="{Binding artist}" Foreground="{StaticResource RedBrush}" FontFamily="Segoe WP" FontSize="21.333" VerticalAlignment="Top" Margin="111,55,0,0"/>
<TextBlock Name="price" Text="{Binding price}"  Foreground="#FF6E6E6E" FontFamily="Segoe WP"   FontSize="20.8" VerticalAlignment="Bottom"  Margin="111,0,0,8" /> 

I mean simply remove DataContext attributes from these tags . 我的意思是从这些tags删除DataContext属性。 Once more, I'm not so sure for this, but just try it & let me know. 再一次,我不太确定是否要这样做,但请尝试一下并让我知道。 Including Binding and DataContext both in same tag looked odd to me. 在同一标签中包含BindingDataContext都让我感到奇怪。 I never did this before. 我以前从未做过。 I may not be correct. 我可能不正确。 But still you can try this. 但您仍然可以尝试一下。 I hope this will help. 我希望这将有所帮助。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM