Wpf 每次更改选择时,选项卡项都会加载用户控件

Wpf 每次更改选择时,选项卡项都会加载用户控件,wpf,vb.net,tabs,tabcontrol,Wpf,Vb.net,Tabs,Tabcontrol,我有一个TabControl,我在某些按钮单击时将用户控件添加到它的TabItem中。但是,当我更改所选选项卡时,每当它再次加载UserControl时,就会出现一个问题。为什么会发生这种情况 我使用以下代码在其中附加UserControl: Dim MyControl As UserControl1 = New UserControl1 tabItem1.Content = MyControl 问题在哪里?我希望它只在第一次加载UserControl,而不是每次更改所选选项卡时再次加载?解决

我有一个TabControl,我在某些按钮单击时将用户控件添加到它的TabItem中。但是,当我更改所选选项卡时,每当它再次加载UserControl时,就会出现一个问题。为什么会发生这种情况

我使用以下代码在其中附加UserControl:

Dim MyControl As UserControl1 = New UserControl1
tabItem1.Content = MyControl
问题在哪里?我希望它只在第一次加载UserControl,而不是每次更改所选选项卡时再次加载?解决方案是什么

tabControl的Xaml如下所示:

      <TabControl Name="MainTab" Height="Auto" HorizontalAlignment="Stretch"  Margin="20,166,0,0" 
                VerticalAlignment="Stretch"  Width="Auto" Background="Gray"  >
        <TabItem Header="first tab" Name="TabItem1" Style="{StaticResource TabStyle}">

            <Grid>
                <Button Content="Button" Height="116" HorizontalAlignment="Left" Margin="385,311,0,0" Name="Button2" VerticalAlignment="Top" Width="217" />
            </Grid>
        </TabItem>
    </TabControl>

根据您在
TabControl
上设置项目的方式(通过
ItemsSource
或内联
TabItem
s),除所选项目外,它将放弃所有
TabItem
VisualTree
。张贴您的
XAML
以查看如何设置
TabItem
s。检查此答案[设置标志以避免反复加载][1][1]:
    Imports System.Windows.Controls
    Imports System.Windows
    Imports System.Windows.Input
     Imports System.Windows.Media
   Class ClosableTab
    Inherits TabItem
    ' Constructor
   Public Sub New()
    ' Create an instance of the usercontrol
    Dim closableTabHeader As New CloseableHeader()
    '
    ' Assign the usercontrol to the tab header
    Me.Header = closableTabHeader
    Me.BorderBrush = Brushes.Pink
    Me.BorderThickness = New Thickness(2, 2, 2, 2)
    ' Attach to the CloseableHeader events (Mouse Enter/Leave, Button Click, and Label resize)
    Me.Style = Application.Current.FindResource("TabStyle")
           AddHandler closableTabHeader.button_close.MouseEnter, AddressOf button_close_MouseEnter
           AddHandler closableTabHeader.button_close.MouseLeave, AddressOf button_close_MouseLeave
           AddHandler closableTabHeader.button_close.Click, AddressOf button_close_Click
           AddHandler closableTabHeader.label_TabTitle.SizeChanged, AddressOf label_TabTitle_SizeChanged

End Sub
''' <summary>
''' Property - Set the Title of the Tab
''' </summary>
Public WriteOnly Property Title() As String
    Set(value As String)
        DirectCast(Me.Header, CloseableHeader).label_TabTitle.Content = value
    End Set
End Property

' Override OnSelected - Show the Close Button
Protected Overrides Sub OnSelected(e As RoutedEventArgs)
    MyBase.OnSelected(e)
    DirectCast(Me.Header, CloseableHeader).button_close.Visibility = Visibility.Visible
End Sub

' Override OnUnSelected - Hide the Close Button
Protected Overrides Sub OnUnselected(e As RoutedEventArgs)
    MyBase.OnUnselected(e)
    DirectCast(Me.Header, CloseableHeader).button_close.Visibility = Visibility.Hidden
End Sub

' Override OnMouseEnter - Show the Close Button
Protected Overrides Sub OnMouseEnter(e As MouseEventArgs)
    MyBase.OnMouseEnter(e)
    DirectCast(Me.Header, CloseableHeader).button_close.Visibility = Visibility.Visible
    'Me.Background = New SolidColorBrush(DirectCast(ColorConverter.ConvertFromString("#B2FFFF"), Color))
End Sub

' Override OnMouseLeave - Hide the Close Button (If it is NOT selected)
Protected Overrides Sub OnMouseLeave(e As MouseEventArgs)
    MyBase.OnMouseLeave(e)
    If Not Me.IsSelected Then
        DirectCast(Me.Header, CloseableHeader).button_close.Visibility = Visibility.Hidden

    End If
End Sub

' - - - Event Handlers  - - -

' Button MouseEnter - When the mouse is over the button - change color to Red
Private Sub button_close_MouseEnter(sender As Object, e As MouseEventArgs)
    DirectCast(Me.Header, CloseableHeader).button_close.Foreground = Brushes.White
End Sub

' Button MouseLeave - When mouse is no longer over button - change color back to black
Private Sub button_close_MouseLeave(sender As Object, e As MouseEventArgs)
    DirectCast(Me.Header, CloseableHeader).button_close.Foreground = Brushes.Black
End Sub


' Button Close Click - Remove the Tab - (or raise an event indicating a "CloseTab" event has occurred)
Private Sub button_close_Click(sender As Object, e As RoutedEventArgs)
    DirectCast(Me.Parent, TabControl).Items.Remove(Me)
End Sub


' Label SizeChanged - When the Size of the Label changes (due to setting the Title) set position of button properly
Private Sub label_TabTitle_SizeChanged(sender As Object, e As SizeChangedEventArgs)
    DirectCast(Me.Header, CloseableHeader).button_close.Margin = New Thickness(DirectCast(Me.Header, CloseableHeader).label_TabTitle.ActualWidth + 5, 3, 4, 0)
End Sub

   End Class
     Dim tab As New ClosableTab
    MainTab.Items.Add(tab)