Skip to Content
0
Former Member
Nov 21, 2011 at 12:08 PM

WPF CrystalReportsViewer conflicts with TabControl style

111 Views

Hi,

I am working on a large enterprise application which has a windows desktop client designed in WPF. We wanted to integrate crystal reports in that application. I created a UserControl which hosts the CrystalReportsViewer control and wrote code to load the report in that control. But while rendering the report, the CrystalReportsViewer threw the following exception:-

Error: "Object reference not set to an instance of an object." 

StackTrace:
   at SAPBusinessObjects.WPF.Viewer.ReportAlbum.ShowTabHeader(Boolean isShow)
   at SAPBusinessObjects.WPF.Viewer.ReportAlbum.ItemsChangedEventHandler(Object sender, ItemsChangedEventArgs e)
   at System.Windows.Controls.Primitives.ItemsChangedEventHandler.Invoke(Object sender, ItemsChangedEventArgs e)
   at System.Windows.Controls.ItemContainerGenerator.OnItemAdded(Object item, Int32 index)
   at System.Windows.Controls.ItemContainerGenerator.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
   at System.Windows.Controls.ItemContainerGenerator.System.Windows.IWeakEventListener.ReceiveWeakEvent(Type managerType, Object sender, EventArgs e)
   at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list)
   at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args)
   at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args)
   at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
   ...truncated...

on investigation I found out that a non-keyed style defined for TabControl was the reason for this exception. I removed that style and the CrystalReportsViewer worked fine and showed the report correctly. I think the CrystalReportsViewer control contains a TabControl which doesn't(explicitly) speficies any style and as we have non-keyed style defined in our project which gets automatically applied when the CrystalReportsViewer renders itself. and CrystalReportsViewer expects some elements with some particular name or order which it does not get and it throws exception.

To quickly reproduce this error. create a new WPF project and paste the following style in that:

<Window.Resources>
        <Style  TargetType="{x:Type TabControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <TabPanel 
             Grid.Row="0"
             Panel.ZIndex="1" 
             Margin="0,0,4,-1" 
             IsItemsHost="True"
             Background="Transparent" />
                            <Border 
             Grid.Row="1"
             BorderBrush="Black" 
             BorderThickness="1" 
             CornerRadius="0, 12, 12, 12" >
                                <Border.Background>
                                    <LinearGradientBrush>
                                        <GradientStop Color="LightBlue" Offset="0" />
                                        <GradientStop Color="White" Offset="1" />
                                    </LinearGradientBrush>
                                </Border.Background>
                                <ContentPresenter ContentSource="SelectedContent" />
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border 
                 Name="Border"
                 Background="LightBlue"
                 BorderBrush="Black" 
                 BorderThickness="1,1,1,1" 
                 CornerRadius="6,6,0,0" >
                                <ContentPresenter x:Name="ContentSite"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"
                   ContentSource="Header"
                   Margin="12,2,12,2"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

and place a CrystalReportsViewer control on the same window:

<crystal:CrystalReportsViewer Name="reportViewer" Grid.Row="0" ShowLogo="False" ShowCopyButton="False" ShowOpenFileButton="True"  />

Now, run the application and click on "Open Report" button on the report viewer control, it doesn't display the report and throws an exception.

Please advice.

Thanks,

Jaspreet Singh.

Edited by: jaspreet on Nov 21, 2011 1:09 PM