Question

In my WPF application I have a ResourceDictionary with some Controls:

<ResourceDictionary>
    <Border x:Key="SystemBorder">
        <!-- SomeContent -->
    </Border>

    <Border x:Key="DateBorder">
        <!-- SomeContent -->
    </Border>

    <Button x:Key="QuitButton">
        <!-- SomeContent -->
    </Button>
</ResourceDictionary>

Now in my Window I have a TabControl with some TabItem, inside of every TabItem that Controls are in use.
But when I change Tabs everything is fine, but when I change back to a Tab, these Controls won't be there.

I tried to use x:Shared="False" but it made no difference.
How can I solve that problem?

Edit:
Every TabItem has the same structure:

<TabItem>
    <Grid>
        <Grid.RowDefinitions/>
            <!-- Defs -->
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions/>
            <!-- Defs -->
        </Grid.ColumnDefinitions>
    </Grid>

        <Border x:Name="Menu1" Grid.Row="0" Grid.Column="0">
            <ContentControl x:Name="Date1" Content="{DynamicResource DateBorder}">
            <!-- some Buttons -->
            <ContentControl x:Name="QuitButton1" Content="{DynamicResource QuitButton}"/>
        </Border>
        <!-- some more Borders -->
        <ContentControl x:Name="SystemBorder1" Grid.Row="3" Grid.Column="0" Content="{DynamicResource SystemBorder}"/>
</TabItem>

The DateBorder also have a Thread which updates the Date every second.

Was it helpful?

Solution 2

I found a Workaround with Code behind, so I'll share it with you:

C#:

private void TabControl_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
{
    for(int i = 1; i < 4; i++)
    {
        FindName("Date" + i).Content = null;
        FindName("QuitButton" + i).Content = null;
        FindName("SystemBorder" + i).Content = null;
    }

    var tc = (TabControl) sender;
    var index = tc.SelectedIndex + 1;

    FindName("Date" + index).Content = FindResource("DateBorder");
    FindName("QuitButton" + index).Content = FindResource("QuitButton");
    FindName("SystemBorder" + index).Content = FindResource("SystemBorder");
}

VB:

Private Sub TabControl_SelectionChanged(sender As Object, e As SelectionChangedEventArgs)
    For i = 1 To 3
        FindName("Date" & i).Content = Nothing
        FindName("QuitButton" & i).Content = Nothing
        FindName("SystemBorder" & i).Content = Nothing
    Next

    Dim tc = CType(sender, TabControl)
    Dim index = tc.SelectedIndex + 1

    FindName("Date" & index).Content = FindResource("DateBorder")
    FindName("QuitButton" & index).Content = FindResource("QuitButton")
    FindName("SystemBorder" & index).Content = FindResource("SystemBorder")
End Sub

OTHER TIPS

If I were to wager it is probably because the borders cannot be in the visual tree multiple times.

Why don't you bind to a collection and create a DataTemplate for the tabs:

<DataTemplate x:Key="TabTemplate">
    <Grid>
        <Grid.RowDefinitions/>
            <!-- Defs -->
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions/>
            <!-- Defs -->
        </Grid.ColumnDefinitions>
    </Grid>
    <Border Grid.Row="0" Grid.Column="0">
        <!-- some Menu1-->
        <Border>
            <!-- DateBorder-->
        </Border>
        <Button>
            <!-- QuitButton -->
        </Button>
        </Border>
    </Border>
    <!-- some more Borders -->
    <Border Grid.Row="3" Grid.Column="0" >
        <!-- SomeContent -->
    </Border>
</DataTemplate>

<TabControl ItemSource="{Binding Collection}" ItemTemplate="{StaticResource TabTemplate}" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top