Silverlight ListBox ItemContainerStyle Бросает NRE - PullRequest
1 голос
/ 29 апреля 2011

У меня есть ListBox ItemContainerStyle ниже.Он просто обеспечивает приятное постепенное появление и скольжение, когда элементы добавляются или удаляются из ItemsSource соответственно;все работает нормально.

Проблема возникает, когда я помещаю свой список в TabControl из инструментария SL.Когда я загружаю некоторые элементы в свой список, затем перехожу на другую вкладку, затем переворачиваюсь назад, если я удаляю элемент, я вижу, что происходит анимация, а затем, как только она заканчивается, я получаю следующее исключение NullReferenceException:

в System.Windows.Controls.ItemContainerGenerator.LayoutStatesManager._layoutStatesGroups_CurrentStateChanged (отправитель объекта, VisualStateChangedEventArgs e) в MS.Internal.CoreInvokeHandler.InvokeEvent, объектная заявка, объект-делегат.JoltHelper.FireEvent (IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)

Что еще хуже, это происходит только тогда, когда я использую собственный горизонтальный элемент ItemsPanel, т.е.VirtualizingPanel по умолчанию.

Вот полный шаблон:

    <Style x:Key="ListBoxStyleAnimatedAddAndRemove" TargetType="ListBoxItem">
        <Setter Property="Padding" Value="3"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="TabNavigation" Value="Local"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Grid Background="{TemplateBinding Background}" x:Name="rootGridElement">
                        <VisualStateManager.VisualStateGroups>

                            <VisualStateGroup x:Name="LayoutStates">
                                <VisualState x:Name="AfterLoaded">
                                    <Storyboard>
                                        <DoubleAnimation 
                                           Duration="00:00:1" 
                                           From="0" To="1" 
                                           Storyboard.TargetName="rootGridElement"
                                           Storyboard.TargetProperty="Opacity" />
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="BeforeUnloaded" />

                                <VisualStateGroup.Transitions>
                                    <VisualTransition From="AfterLoaded" To="BeforeUnload" GeneratedDuration="0:0:0.5">
                                        <Storyboard>

                                            <DoubleAnimation 
                                                Duration="00:00:0.25" 
                                                By="235" 
                                                Storyboard.TargetName="contentProjection" 
                                               Storyboard.TargetProperty="GlobalOffsetY" />
                                        </Storyboard>
                                    </VisualTransition>
                                </VisualStateGroup.Transitions>
                            </VisualStateGroup>

                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal" />
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".35" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".55" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="contentPresenter"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" To=".75" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="fillColor2"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="FocusStates">
                                <VisualState x:Name="Focused">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetProperty="Visibility" Storyboard.TargetName="FocusVisualElement">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <Visibility>Visible</Visibility>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Unfocused"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Rectangle x:Name="fillColor" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
                        <Rectangle x:Name="fillColor2" Fill="#FFBADDE9" IsHitTestVisible="False" Opacity="0" RadiusY="1" RadiusX="1"/>
                        <ContentPresenter x:Name="contentPresenter" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}">
                            <ContentPresenter.Projection>
                                <PlaneProjection x:Name="contentProjection">
                                </PlaneProjection>
                            </ContentPresenter.Projection>
                        </ContentPresenter>
                        <Rectangle x:Name="FocusVisualElement" RadiusY="1" RadiusX="1" Stroke="#FF6DBDD1" StrokeThickness="1" Visibility="Collapsed"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
...