これは実用的な例です。
Xaml:
<Window x:Class="WpfApplicationUpper.Window3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window3" Height="300" Width="300" SizeToContent="WidthAndHeight">
<Grid>
<TabControl Name="MainTabControl"
SelectionChanged="MainTabControl_SelectionChanged"
PreviewMouseDown="MainTabControl_PreviewMouseDown">
<TabItem Header="Small Tab" >
<Border Background="AliceBlue" Width="200" Height="200" />
</TabItem>
<TabItem Header="Medium Tab">
<Border Background="Blue" Width="400" Height="400" />
</TabItem>
<TabItem Header="Large Tab">
<Border Background="Navy" Width="600" Height="600" />
</TabItem>
</TabControl>
</Grid>
</Window>
背後にあるコード:
public partial class Window3 : Window
{
public Window3() {InitializeComponent();}
double _currentWidth;
double _currentHeight;
private void MainTabControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
TabItem currentItem = MainTabControl.SelectedItem as TabItem;
FrameworkElement content = currentItem.Content as FrameworkElement;
_currentWidth = content.ActualWidth;
_currentHeight = content.ActualHeight;
}
private void MainTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabItem itemAdded = null;
TabItem itemRemoved = null;
if (e.AddedItems.Count > 0)
itemAdded = e.AddedItems[0] as TabItem;
if (e.RemovedItems.Count > 0)
itemRemoved = e.RemovedItems[0] as TabItem;
if (itemAdded != null && itemRemoved != null)
{
FrameworkElement content = itemAdded.Content as FrameworkElement;
double newWidth = content.Width;
double newHeight = content.Height;
if (newWidth < _currentWidth)
content.Width = _currentWidth;
if (newHeight < _currentHeight)
content.Height = _currentHeight;
}
}
}
私はこれが少し醜いことを知っていますが、何もない方が良いです:)