样式化ScrollViewer导致null异常

时间:2014-03-20 20:45:01

标签: wpf xaml scrollviewer

我重新设置了SurfaceScrollViewer样式但是每当我双击轨道区域时它似乎会抛出NullPointerException ...通过异常的外观,我相信它与CommandBinding有关为空。

A first chance exception of type 'System.NullReferenceException' occurred in Microsoft.Surface.Presentation.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>Sendx.Cabo.Analyzer.Application.exe</AppDomain><Exception><ExceptionType>System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType><Message>Object reference not set to an instance of an object.</Message><StackTrace>   at Microsoft.Surface.Presentation.Controls.SurfaceScrollViewer.OnScrollPage(OrientationFlags orientation, Boolean isIncrease)
   at Microsoft.Surface.Presentation.Controls.SurfaceScrollViewer.OnScrollPageDownCommand(Object target, ExecutedRoutedEventArgs args)
   at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
   at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
   at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
   at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
   at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e) ...

这是风格的主要部分。是否有一些我错过的模板绑定导致了这个问题?

<Style x:Key="ScrollViewerStyle" TargetType="{x:Type controls:SurfaceScrollViewer}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ScrollViewer}">
                <Grid x:Name="Grid" Background="{TemplateBinding Background}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Rectangle x:Name="Corner" Grid.Column="1" Fill="{x:Null}" Grid.Row="1"/>
                    <ScrollContentPresenter x:Name="PART_ScrollContentPresenter" 
                                            CanContentScroll="{TemplateBinding CanContentScroll}"   
                                            CanHorizontallyScroll="False" 
                                            CanVerticallyScroll="False"   
                                            ContentTemplate="{TemplateBinding ContentTemplate}"   
                                            Content="{TemplateBinding Content}" Grid.Column="0"   
                                            Margin="{TemplateBinding Padding}" 
                                            Grid.Row="0"
                                            OverridesDefaultStyle="True"/>
                    <ScrollBar x:Name="PART_VerticalScrollBar" 
                               Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"   
                               AutomationProperties.AutomationId="VerticalScrollBar" 
                               Cursor="Arrow" 
                               Grid.Column="1"   
                               Maximum="{TemplateBinding ScrollableHeight}"
                               Minimum="0" 
                               Grid.Row="0"   
                               Value="{Binding VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"   
                               ViewportSize="{TemplateBinding ViewportHeight}" 
                               Style="{DynamicResource VerticalScrollStyle}"   
                               Background="{x:Null}" Width="Auto" Margin="0"/>
                    <ScrollBar x:Name="PART_HorizontalScrollBar" 
                               Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}"   
                               AutomationProperties.AutomationId="HorizontalScrollBar" 
                               Cursor="Arrow" 
                               Grid.Column="0"   
                               Maximum="{TemplateBinding ScrollableWidth}" 
                               Minimum="0" 
                               Orientation="Horizontal" 
                               Grid.Row="1"   
                               Value="{Binding HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"   
                               ViewportSize="{TemplateBinding ViewportWidth}" 
                               Style="{DynamicResource HorizontalScrollStyle}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:0)

使用SurfaceScrollBar样式中的SurfaceScrollView而不是标准ScrollBar修复了问题。