0


I have a system of closable tabs, and each tab has RichTextbox (as a scratchpad). The problem I am having is that whenever I create a new tab it should supposedly create a new RichTextBox, but it overwrites the contents of all the previous RTBs, clearing them all. Can someone tell me why this happens?? It doesn't happen however when I create fixed RTBs (eg create a tab control with 4 tabs and each tab has its own RTB), then everything works fine. but it doesn't work for dynamic tabs?

The code below is from a Usercontrol that is contained within each tab item:

    <Border Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="0" Grid.RowSpan="3" BorderBrush="#FF939393" BorderThickness="26" CornerRadius="26" >
        <Border.Effect>
            <DropShadowEffect />
        </Border.Effect>           
    </Border>
    <TextBlock Grid.Column="1" Grid.Row="0" Height="23" Name="textBlock1" Text="{Binding Path=TestMessage}" />
    <Button Grid.Column="0" Grid.Row="1" Style="{DynamicResource PageNavigationButton}" Height="52" Width="26" Command="{Binding Path=CMD_SwapLeft}" >
        <Button.RenderTransform >
            <RotateTransform Angle="180" CenterX="13" CenterY="26" />
        </Button.RenderTransform>
    </Button>
    <Button Grid.Column="2" Grid.Row="1" Style="{DynamicResource PageNavigationButton}" Height="52" Width="26" Command="{Binding Path=CMD_SwapRight}" />

    <View:DebtorTabView Grid.Column="1" Grid.Row="1" Visibility="{Binding RelativeSource={RelativeSource Self}, Path=Parent.DataContext.MyDataContextVisibility}"  DataContext="{Binding Path=MyDataContext}"  />
    <Grid Background="Blue" Grid.Column="1" Grid.Row="1" Visibility="{Binding Path=MyDataContextOtherVisibility}">
        <TextBlock Text="{Binding Path=TestMessageTwo}" Height="23" Width="124" Margin="6,6,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" />
        <RichTextBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
    </Grid>

I haven't done any of the bindings done(which is part of a custom control when I sort out this problem).
Thanks All

Heinrich
  • 2,144
  • 3
  • 23
  • 39
  • Can you include the code where you create the dynamic tabs as well? Thanks – SalGad Jun 27 '12 at 05:57
  • SalGad, the are add the same way as in this article by Josh Smith:http://msdn.microsoft.com/en-us/magazine/dd419663.aspx – Heinrich Jun 27 '12 at 21:34

1 Answers1

1

This is because WPF will unload TabItems that are not in use, which means if the Text property is not bound to anything, it will get reset.

Once you bind your RichTextBox.Text property to something in the DataContext, it should work fine.

An alternative is to extend the current TabControl class so it doesn't destroy its children when you switch tabs. You can find the code for this in this SO answer (the site that originally contained the code is not around anymore), and you use the control in the XAML the same way as you would a regular TabControl

<local:TabControlEx ItemsSource="{Binding OpenTabs}" 
                    SelectedTab="{Binding SelectedTab}" />
Community
  • 1
  • 1
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • I thought that might be the case Thanks :) although the other interesting thing that is happening now is that i create one tab write in the RTB then create a new tab the contents of the RTB in the first tab appears in the RTB of the second tab :/ – Heinrich Jun 27 '12 at 23:01
  • Thank You After a "bit" of tweaking I believe I have sorted it :) – Heinrich Jun 28 '12 at 00:47