我终于自己实现了一个简单的 tabcontrol,因为这样我就可以控制一切了。
<WrapPanel x:Name="WrapPanel_Main"> <!-- This is the TabControl -->
<Border x:Name="Border_Configuration" Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the first tab -->
<TextBlock x:Name="TextBlock_Configuration" Text="Configuration" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
</Border>
<Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the second tab -->
<TextBlock x:Name="TextBlock_Artists" Text="Artists" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp" />
</Border>
<Border Margin="5,5,0,0" BorderThickness="4,4,4,0"> <!-- This is the third tab -->
<TextBlock x:Name="TextBlock_ReleaseGroups" Text="Release Groups" Padding="5" MouseLeftButtonUp="TextBlock_Step_MouseLeftButtonUp"/>
</Border>
</WrapPanel>
<Border x:Name="Border_Placeholder" Grid.Row="1" Margin="5,0,5,5"> <!-- placeholder for the content of each tab -->
<ContentControl x:Name="ContentControl_Placeholder" Grid.Row="1" Padding="5" />
</Border>
这里是处理“选项卡”的 Mouse-Up-Event 的处理程序。我创建了一个界面,每个用作选项卡内容的用户控件都必须实现。这允许用户控件通知“选项卡控件”有关未保存的更改并采取适当的(用户选择)操作。
之后,它会加载新内容并更改“Tab-Headers”的外观。在我看来,这导致的更多代码量对于选项卡更改的完全控制是可以接受的。
private void TextBlock_Step_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
ICancelUnloading currentElement = ContentControl_Placeholder.Content as ICancelUnloading;
if (currentElement != null)
{
if (currentElement.UnsavedChanges)
{
MessageBoxResult result = MessageBox.Show("Yes: Save, No: Discard, Cancel: Stay", "Unsaved Changes", MessageBoxButton.YesNoCancel, MessageBoxImage.Warning, MessageBoxResult.Cancel);
if (result == MessageBoxResult.Cancel)
return;
if (result == MessageBoxResult.Yes)
currentElement.Save();
}
}
TextBlock textBlock = sender as TextBlock;
if (textBlock != null)
{
switch (textBlock.Name)
{
case "TextBlock_Configuration":
ContentControl_Placeholder.Content = new ConfigurationView();
break;
case "TextBlock_Artists":
ContentControl_Placeholder.Content = new ArtistsView();
break;
case "TextBlock_ReleaseGroups":
ContentControl_Placeholder.Content = new ReleaseGroupsView();
break;
}
ActivateTab(textBlock);
}
}