Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight 3 Checkbox Listbox bug when scrolling?

I've spent a few minutes searching on Google and have not found anything related to this issue I'm having:

Today I upgraded to the Silverlight 3 SDK and converted a project that I'm working on. I then noticed a bug in my program with a Listbox that has a Checkbox as its DataTemplate.

When one or more items is checked, and I scroll up and down, it seems that a few of the Checkboxes at the extremes get checked off and on randomly. This does not trigger the Checked/Unchecked event, however.

Has anyone seen this behavior? I'm not doing anything out of the ordinary, simply scrolling up and down once at least one checkbox has been checked, and a couple of others that I have not touched seem to get checked on and off repeatedly. This was definitely not happening with the Silverlight 2 SDK.

Here's the XAML definition for my Listbox:

<ListBox x:Name="cBoxSalesmen" Width="135" Height="200" 
 HorizontalAlignment="Left" VerticalAlignment="Top">
<ListBox.Template>
    <ControlTemplate>
        <Border Style="{StaticResource BorderStyleThin}">
            <StackPanel Orientation="Vertical">
                <TextBlock Text="Salesmen" />
                <ScrollViewer Height="176" VerticalScrollBarVisibility="Visible" >
                    <ItemsPresenter />
                </ScrollViewer>
            </StackPanel>
        </Border>
    </ControlTemplate>
</ListBox.Template>
<ListBox.ItemTemplate>
    <DataTemplate>
        <CheckBox Margin="0" Content="{Binding}" FontSize="10" HorizontalAlignment="Left"
              Checked="SalesmenCheckbox_Checked" Unchecked="SalesmenCheckbox_Unchecked"/>
    </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
like image 281
Overhed Avatar asked Oct 09 '09 19:10

Overhed


1 Answers

The default ItemsPanel of the ListBox is the VirtualizingStackPanel. You can change it to use the StackPanel, this way you problem is solved. Use this code:

<ListBox.ItemsPanel>
  <ItemsPanelTemplate>
     <StackPanel />
  </ItemsPanelTemplate>
<ListBox.ItemsPanel>
like image 180
Murilo Amaru Gomes Avatar answered Sep 19 '22 20:09

Murilo Amaru Gomes