This is a migrated thread and some comments may be shown as answers.

Exception thrown when switching tabs

5 Answers 102 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Trude
Top achievements
Rank 2
Trude asked on 27 Apr 2012, 09:10 AM
I have a TabControl with an ItemTemplate and the tabs content is defined in a ContentTemplate. Basically each tab contains a couple of RichTextBoxes (standard MS). See screenshot #1. This works fine; I can databind an arbitrary number of tabs and switch between them. But when there is a hyperlink/URL in the XAML of the RichTextBox the application throws an exception when I switch to or from this tab (never on first load). From attached screenshot #2 it seems that the TabControl is at fault, but I'm not sure. I'm unable to provoke this behaviour if there is no hyperlink in the XAML.

The TabControl is defined as such:
<telerik:RadTabControl x:Name="RadTabControl2" IsContentPreserved="True"
                        ItemTemplate="{StaticResource TabControlItemTemplate}"
                        ContentTemplate="{StaticResource TabControlContentTemplate}"
                        SelectionChanged="RadTabControl1_SelectionChanged" OverflowMode="Wrap"/>

TabControlItemTemplate:
<DataTemplate x:Key="TabControlItemTemplate">
    <StackPanel Orientation="Horizontal" ToolTipService.ToolTip="{Binding dimensions_tab.description}" MinHeight="21">
        <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
            <Image Source="{Binding Converter={StaticResource TabIconConverter}, Path=ItemState}" Stretch="Fill" Width="18" Height="18"/>
            <TextBlock Text="{Binding dimensions_tab.name}" Margin="3,0,8,0" TextWrapping="Wrap" TextAlignment="Left"/>
        </StackPanel>
        <telerik:RadButton Visibility="{Binding ItemVisibility}" Click="ButtonDelete_Click" MouseEnter="RadButton_MouseEnter" MouseLeave="RadButton_MouseLeave"
                           BorderBrush="{x:Null}" Background="{x:Null}" IsTabStop="False" VerticalAlignment="Center"
                           ToolTipService.ToolTip="{Binding LocStr.Delete_item, Source={StaticResource LocStr}}">
            <telerik:RadButton.Content>
                <Image x:Name="CloseImage" VerticalAlignment="Center" Width="14" Height="14" Stretch="Fill" Source="close_tab_inactive.png"/>
            </telerik:RadButton.Content>
        </telerik:RadButton>
    </StackPanel>
</DataTemplate>

TabControlContentTemplate:
<DataTemplate x:Key="TabControlContentTemplate">
    <ScrollViewer VerticalScrollBarVisibility="Auto" BorderThickness="0">
        <Grid Margin="6,4,6,4">
            <Grid Visibility="{Binding ItemState, Converter={StaticResource AddTabVisibleConverter}, ConverterParameter=BoundTab}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <StackPanel Orientation="Vertical">
                    <Grid>
                        <TextBlock Text="{Binding LocStr.Question, Source={StaticResource LocStr}}" HorizontalAlignment="Left" Style="{StaticResource TextBlockNormal}"/>
                        <HyperlinkButton Click="ButtonEditQuestion_Click" Content="{Binding LocStr.Edit, Source={StaticResource LocStr}}" HorizontalAlignment="Right" Foreground="Blue" IsTabStop="False" Tag="{Binding ElementName=RTBQuestion}"/>
                    </Grid>
                    <RichTextBox x:Name="RTBQuestion" Loaded="RTBQuestion_Loaded" IsReadOnly="True" IsTabStop="False"/>
                </StackPanel>
                <StackPanel Orientation="Vertical" Grid.Row="1" Margin="0,5,0,0">
                    <Grid>
                        <TextBlock Text="{Binding LocStr.Guidance, Source={StaticResource LocStr}}" HorizontalAlignment="Left" Style="{StaticResource TextBlockNormal}"/>
                        <HyperlinkButton Click="ButtonEditGuidance_Click" Content="{Binding LocStr.Edit, Source={StaticResource LocStr}}" HorizontalAlignment="Right" Foreground="Blue" IsTabStop="False" Tag="{Binding ElementName=RTBGuidance}"/>
                    </Grid>
                    <RichTextBox x:Name="RTBGuidance" Loaded="RTBGuidance_Loaded" IsReadOnly="True" IsTabStop="False"/>
                </StackPanel>
                <StackPanel Orientation="Vertical" Grid.Row="2" Margin="0,5,0,0">
                    <local:document_explorer x:Name="DocumentList" Loaded="DocumentList_Loaded"></local:document_explorer>
                </StackPanel>
            </Grid>
            <Grid Visibility="{Binding ItemState, Converter={StaticResource AddTabVisibleConverter}, ConverterParameter=AddTab}" Loaded="UpdateAddTab">
                <TextBlock x:Name="TextIndicator" Text="{Binding LocStr.Question_add_indicator, Source={StaticResource LocStr}}" Style="{StaticResource TextBlockNormal}" Visibility="Collapsed"/>
                <StackPanel x:Name="StackDimension" Orientation="Vertical">
                    <TextBlock Text="{Binding LocStr.Question_add_dimension, Source={StaticResource LocStr}}" Style="{StaticResource TextBlockNormal}"/>
                    <ItemsControl x:Name="ItemsControlDimension">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Orientation="Vertical"/>
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <telerik:RadButton Click="RadButton_Click" IsTabStop="False" HorizontalContentAlignment="Left" Margin="0,2,0,0" ToolTipService.ToolTip="{Binding description}">
                                    <StackPanel Orientation="Horizontal">
                                        <Image Source="/IntRisk2;Component/Images/bd_dimensions.png" Stretch="Fill" Width="18" Height="18" Margin="5,0,3,0"/>
                                        <TextBlock Text="{Binding name}"/>
                                    </StackPanel>
                                </telerik:RadButton>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </Grid>
        </Grid>
    </ScrollViewer>
</DataTemplate>


5 Answers, 1 is accepted

Sort by
0
Trude
Top achievements
Rank 2
answered on 27 Apr 2012, 09:36 AM
I have created a very simple scenario to reproduce this behavior. Basically I have narrowed it down to two things:

1) If I remove the ScrollViewer from the ContentTemplate the exception goes away
2) If I set the IsContentPreserved to False the exception also goes away

Below is the XAML of my test scenario. Execute it and switch between tabs OK. Press the button, which only inserts a hyperlink element, switch to the empty tab and the exception occurs. Apply either of the above two "fixes" and the exception goes away.
<telerik:RadWindow
    x:Class="IntRisk2.test_error_window"
    xmlns:local="clr-namespace:IntRisk2"
 
    <Grid x:Name="LayoutRoot">
         
        <telerik:RadTabControl IsContentPreserved="True" OverflowMode="Wrap">
            <telerik:RadTabItem Header="Tab1">
                <ScrollViewer VerticalScrollBarVisibility="Auto" BorderThickness="0">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <Button Content="Insert hyperlink" Click="Button_Click"/>
                        <RichTextBox Grid.Row="1" Name="RTB1" IsReadOnly="True" IsTabStop="False"/>
                    </Grid>
                </ScrollViewer>
            </telerik:RadTabItem>
            <telerik:RadTabItem Header="Tab2">
            </telerik:RadTabItem>
        </telerik:RadTabControl>
         
    </Grid>
 
</telerik:RadWindow>
0
Hristo
Telerik team
answered on 27 Apr 2012, 02:39 PM
Hi Jorn,

Currently we have an issue when using hyperlink inside TabControl (more precisely, inside TabItem content). When the hyperlink object receives the focus and the selection is changed an exception will be raised just as you have explained. We recommend our customers to use hyperlink buttons instead.
I have exposed the bug in our public issue tracking system. You could track (in order to be notified when the issue is resolved) or vote for its resolution via following link: http://www.telerik.com/support/pits.aspx#/public/silverlight/10907 

All the best,
Hristo
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Trude
Top achievements
Rank 2
answered on 27 Apr 2012, 04:02 PM
Why does setting IsContentPreserved to false or removing the Scrollviewer help? (at least in my case)
0
Hristo
Telerik team
answered on 01 May 2012, 07:47 AM
Hi,

IsContentPreserved property changes the way TabControl initialized and maintains its content (actually the content of its items). When IsContentPreserved is set to true the visual objects for every selected tab item will be placed in their own content presenter. This means that visuals for content of every already selected item would be in the visual tree. These visuals will not be removed from the visual tree until the item is removed from the items collection.
In other hand, TabControl performs a check when selected item is changed in order to determine whether the focus should be moved to the newly selected item or its content. Thus the user will be able to use the keyboard and navigate using only keyboard.
When tab control performs that check and the focus is hold by hyperlink our code for detecting the focused control will throw an exception because hyperlink is not a visual UIElement.
If IsContentPreserved is set to false the visuals of previously selected item will be removed from the visual tree upon selection and the focus detection will not identify the hyperlink as a focused element.

Just to clarify that in my sample project the issue is reproduced with or without using the ScrollViewer. 

Greetings,
Hristo
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Trude
Top achievements
Rank 2
answered on 07 May 2012, 06:33 PM
Ok, thanks for the detailed explanation - I will update my code and set IsContentPreserved = False for now.
Tags
TabControl
Asked by
Trude
Top achievements
Rank 2
Answers by
Trude
Top achievements
Rank 2
Hristo
Telerik team
Share this question
or