Я использую WPF с шаблоном MVVM.Как изменить размер WPF TabControl при изменении размера главного окна вручную?TabControl также должен изменить размер при максимизации или восстановлении.Можете ли вы объяснить логику как поставить решение?Можете ли вы предложить мне прочитать тему, которая связана с этой логикой.В приведенном ниже коде я являюсь TabControl, чтобы получить внешний вид MDI, где UserControl добавляется в TabControl.
DashboardView.xaml
<Window x:Class="MyProject.DashboardView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyProject"
Title="Resiable tabcontrol"
Width="1064"
Height="897"
WindowState="Normal"
WindowStartupLocation="CenterOwner">
<Window.Resources>
<local:TabSizeConverter x:Key="tabSizeConverter" />
<Style TargetType="{x:Type TabItem}">
<Setter Property="Width">
<Setter.Value>
<MultiBinding Converter="{StaticResource tabSizeConverter}">
<Binding RelativeSource="{RelativeSource Mode=FindAncestor,
AncestorType={x:Type TabControl}}" />
<Binding RelativeSource="{RelativeSource Mode=FindAncestor,
AncestorType={x:Type TabControl}}" Path="ActualWidth" />
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<DockPanel>
<Menu Name="menu1" VerticalAlignment="Top" Visibility="Visible"
DockPanel.Dock="Top" Loaded="menu1_Loaded">
<Menu.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,0">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0" Color="White"/>
<GradientStop Offset="0" Color="White"/>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Menu.Background>
<MenuItem Header="File" Name="mnuTab" >
<MenuItem Header="List of High School"
Click="mnuTab1_Click"/>
<MenuItem Header="List of University"
Click="mnuTab2_Click" InputGestureText="Ctrl-U" />
<MenuItem Header="Exit" Click="mnuExit_Click" />
</MenuItem>
</Menu>
<Grid Width="Auto" Height="Auto" >
<TabControl Name="tcMdi" Visibility="Visible" HorizontalAlignment="Left"
VerticalAlignment="Top" ScrollViewer.HorizontalScrollBarVisibility="Auto"
ScrollViewer.VerticalScrollBarVisibility="Auto" >
<TabControl.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,0">
<LinearGradientBrush.GradientStops>
<GradientStop Offset="0" Color="White" />
<GradientStop Offset="0" Color="White" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</TabControl.Background>
</TabControl>
</Grid>
</DockPanel>
</Window>
DashboardView.xaml.cs
namespace MyProject
{
public partial class DashboardView : Window
{
private Dictionary<string, string> _mdiChildren = new Dictionary<string, string>();
public DashboardView()
{
InitializeComponent();
WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
}
private void mnuTab1_Click(object sender, RoutedEventArgs e)
{
ListOfHighSchoolView mdiChild = new ListOfHighSchoolView ();
AddTab(mdiChild);
txtBackground.Visibility = Visibility.Hidden;
}
private void mnuTab2_Click(object sender, RoutedEventArgs e)
{
ListOfHighUniversityView mdiChild = new ListOfHighUniversityView ();
AddTab(mdiChild);
txtBackground.Visibility = Visibility.Hidden;
}
private void mnuExit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
// Add tab item to the tab
//This is the user control
private void AddTab(ITabbedMDI mdiChild)
{
//Check if the user control is already opened
if (_mdiChildren.ContainsKey(mdiChild.UniqueTabName))
{
//user control is already opened in tab.
//So set focus to the tab item where the control hosted
foreach (object item in tcMdi.Items)
{
TabItem ti = (TabItem)item;
if (ti.Name == mdiChild.UniqueTabName)
{
ti.Focus();
break;
}
}
}
else
{
//the control is not open in the tab item
tcMdi.Visibility = Visibility.Visible;
tcMdi.Width = this.ActualWidth;
tcMdi.Height = this.ActualHeight;
((ITabbedMDI)mdiChild).CloseInitiated += new delClosed(CloseTab);
//create a new tab item
TabItem ti = new TabItem();
//set the tab item's name to mdi child's unique name
ti.Name = ((ITabbedMDI)mdiChild).UniqueTabName;
//set the tab item's title to mdi child's title
ti.Header = ((ITabbedMDI)mdiChild).Title;
//set the content property of the tab item to mdi child
ti.Content = mdiChild;
ti.HorizontalContentAlignment = HorizontalAlignment.Stretch;
ti.VerticalContentAlignment = VerticalAlignment.Top;
//add the tab item to tab control
tcMdi.Items.Add(ti);
//set this tab as selected
tcMdi.SelectedItem = ti;
//add the mdi child's unique name in the open children's name list
_mdiChildren.Add(((ITabbedMDI)mdiChild).UniqueTabName, ((ITabbedMDI)mdiChild).Title);
}
}
private void CloseTab(ITabbedMDI tab, EventArgs e)
{
TabItem ti = null;
foreach (TabItem item in tcMdi.Items)
{
if (tab.UniqueTabName == ((ITabbedMDI)item.Content).UniqueTabName)
{
ti = item;
break;
}
}
if (ti != null)
{
_mdiChildren.Remove(((ITabbedMDI)ti.Content).UniqueTabName);
tcMdi.Items.Remove(ti);
txtBackground.Visibility = Visibility.Visible;
}
}
// Adjust the tab height and weight during load
private void menu1_Loaded(object sender, RoutedEventArgs e)
{
tcMdi.Width = this.ActualWidth;
tcMdi.Height = this.ActualHeight - 10;
}
}
}
Converters.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
namespace MyProject
{
public class TabSizeConverter : System.Windows.Data.IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
TabControl tabControl = values[0] as TabControl;
double width = tabControl.ActualWidth / tabControl.Items.Count;
//Subtract 1, otherwise we could overflow to two rows.
return (width <= 1) ? 0 : (width - 1);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
}