我有一个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>
这个tabitem默认设置,我从后面的代码添加新的tabitems。 Tabitem来自我自己的班级。这是该类的代码:
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
我正在这个类中添加tabitem:
Dim tab As New ClosableTab
MainTab.Items.Add(tab)