wpfxamltabitemresource-file

Disappearing Resource Controls?


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.


Solution

  • 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