当您手动调整窗口大小,最大化或恢复时,如何调整TabControl的大小

时间:2011-11-01 04:03:28

标签: wpf mvvm resize tabcontrol

我正在使用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();         
    }     
  } 
}

1 个答案:

答案 0 :(得分:6)

为什么不从TabControl中删除对齐和大小设置并让它自动调整大小?

如果你不想让它占据整个空间,你可以在它上面设置边距,或者使用网格控件和“*”作为行\列宽\高度(一个高度为“0.5”的GridRow) *“只会占用可用空间的一半。”