我終於可以自己實現一個簡單的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>
這裏,是以 「標籤」 的鼠標向上事件的照顧處理。我創建了一個接口,每個用作該選項卡內容的用戶控件必須實現。這允許用戶控制通知「選項卡控制」關於未保存的更改並採取適當的(用戶選擇)動作。 之後,它加載新內容並更改「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);
}
}
的xmlns:INTR = 「CLR-名稱空間:System.Windows.Interactivity;裝配= System.Windows.Interactivity」 – leapold
有趣的可能性。我不確定mouseup事件是否足以覆蓋改變標籤的所有可能的方式,然後我仍然需要防止標籤改變... – Aaginor