简体   繁体   中英

Make TabControl Width equal to Max TabItems Width and Height

I have a TabControl with many TabItems inside. I am trying to make TabControl's width equal to Max of its TabItems Width (same for Height). I cannot set it explicitly, because my program is multilanguage, and so depending on language selected, I need different widths to all of my labels(they all are in "auto").

Is there a way to do in only in XAML?

Edit :

I found a part of solution in order to do it in the code : When I open my window I do the following :

List<TabItem> list = new List<TabItem>();
list.AddRange(tabControl.Items.OfType<TabItem>());
foreach (TabItem tabItem in list)
{
    tabControl.SelectedItem=tabItem;
    tabControl.UpdateLayout();
    tabItem.UpdateLayout();
    System.Threading.Thread.Sleep(250);
    maxWidth = Math.Max(maxWidth, tabItem.ActualWidth);
    maxHeight = Math.Max(maxHeight, tabItem.ActualHeight);
}
tabControl.Width = maxWidth;
tabControl.Height = maxHeight;

It seems to be a working solution, but I don't understand why tabItem.ActualWidth and tabItem.ActualHeight are always set to 0?

To understand better, I put a screenshot of my window :

选项

When I open my window, I already know in which language it may be opened, so the TabItems dimensions don't change after the windows is loaded.

Edit 2 :

Here is concretely what I mean by "size of TabItems differ regarding used language, this is not the best example as here difference is small, but I would like to apply that to all TabContols of my project to be sure I never met the problem if I make changes in future, or even add another languages :

In Russian :

在此处输入图片说明

In English :

在此处输入图片说明

I finally found a solution, maybe not the best, but it is working perfectly, and didn't find any other solution. It is not possible to do the job when initializing the window, as the contents has not been loaded yet (MyWindow.Show()), so I had to use the SizeChanged event of TabControl.

On my ViewModel I set two parameters TabControlWidth and TabControlHeight .

private double tabControlWidth = 0;
public double TabControlWidth
{
    get { return tabControlWidth; }
    set
    {
        if (this.tabControlWidth != value)
        {
            tabControlWidth = value;
            this.NotifyPropertyChanged("TabControlWidth");
        }
    }
}
private double tabControlHeight = 0;
public double TabControlHeight
{
    get { return tabControlHeight; }
    set
    {
        if (this.tabControlHeight != value)
        {
            tabControlHeight = value;
            this.NotifyPropertyChanged("TabControlHeight");
        }
    }
}

in my file MyWindow.xaml.cs I add a bool property to check when I checked all TabItems(obviously, this property is set to false when initializing the window) :

bool allTabItemsChecked { get; set; }

Finally, I need to add to my TabControl two things :

  • Bind Width and Height to my properties in ViewModel.

  • Add event SizeChanged

    SizeChanged="TabControlSizeChanged" MinHeight="{Binding TabControlHeight}" MinWidth="{Binding TabControlWidth}"

(excuse me, I don't manage to display the last line as code? often happens after I use "-" or "*")

Finally I make the SizeChanged function as following :

private void TabControlSizeChanged(object sender, SizeChangedEventArgs e)
{
    if(this.allTabItemsChecked==false)
    {
        int index = tabControl.SelectedIndex;
        List<TabItem> list = new List<TabItem>();
        list.AddRange(tabControl.Items.OfType<TabItem>());
        if (index < list.Count()-1)
        {
            tabControl.SelectedIndex = index + 1;
        }
        else
        {
            this.allTabItemsChecked = true;
            tabControl.SelectedIndex = 0;
        }
        contexte.TabControlWidth = Math.Max(tabControl.ActualWidth, contexte.TabControlWidth);
        contexte.TabControlHeight = Math.Max(tabControl.ActualHeight, contexte.TabControlHeight);

    }
}

When we display the window, event is automatically launched. I will then change TabControl.SelectedIndex to the next TabItem, and update TabControl dimensions to the bigger one. When I reached the last index, I just set SelectedIndex to 0, then set allTabItemsChecked to true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM