gpt4 book ai didi

wpf - 手动调整窗口大小、最大化或还原时如何调整 TabControl 的大小

转载 作者:行者123 更新时间:2023-12-01 10:08:44 28 4
gpt4 key购买 nike

我正在使用带有 MVVM 模式的 WPF。手动调整主窗口大小时如何调整 WPF TabControl 的大小?当您最大化或还原时,TabControl 也应该调整大小。你能解释一下put解决方案的逻辑吗?您能否建议我阅读与此逻辑相关的主题。在下面的代码中,我是 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();
}
}
}

最佳答案

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

如果您不希望它占据整个空间,您可以在其上设置 Margin 或使用 Grid 控件,将“*”作为行\列宽\高(一个 GridRow,Height="0.5 *"将只占用可用空间的一半)。

关于wpf - 手动调整窗口大小、最大化或还原时如何调整 TabControl 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7962340/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com