C# WPF中工具窗口和内容的大小调整

C# WPF中工具窗口和内容的大小调整,c#,wpf,C#,Wpf,我们有一个VS分机。用户使用工具窗格与扩展进行交互。ToolWindowPane的高度和宽度取决于用户如何设置其VS环境,并且当前ToolWindowPane的内容无法正确调整大小 这是窗口: public class SymCalculationUtilitiesWindow : ToolWindowPane { /// <summary> /// Initializes a new instance of the <see cref="SymCalculat

我们有一个VS分机。用户使用工具窗格与扩展进行交互。ToolWindowPane的高度和宽度取决于用户如何设置其VS环境,并且当前ToolWindowPane的内容无法正确调整大小

这是窗口:

public class SymCalculationUtilitiesWindow : ToolWindowPane
{
    /// <summary>
    /// Initializes a new instance of the <see cref="SymCalculationUtilitiesWindow"/> class.
    /// </summary>
    public SymCalculationUtilitiesWindow() : base(null)
    {
        this.Caption = "Sym Calculation Utilities";

        this.ToolBar = new CommandID(new Guid(Guids.guidConnectCommandPackageCmdSet), Guids.SymToolbar);
        // This is the user control hosted by the tool window; Note that, even if this class implements IDisposable,
        // we are not calling Dispose on this object. This is because ToolWindowPane calls Dispose on
        // the object returned by the Content property.
        this.Content = new UtilitiesView();

    }

}
公共类SYMCLUATIONUtilities窗口:工具窗格
{
/// 
///初始化类的新实例。
/// 
公共SymCalculationUtilitiesWindow():base(null)
{
this.Caption=“Sym计算实用程序”;
this.ToolBar=newcommandid(新Guid(Guids.guidConnectCommandPackageCmdSet)、Guids.SymToolbar);
//这是由工具窗口托管的用户控件;请注意,即使此类实现了IDisposable,
//我们没有对此对象调用Dispose。这是因为ToolWindowPane在上调用Dispose
//内容属性返回的对象。
this.Content=new UtilitiesView();
}
}
因此,UtilitiesView是默认视图。以下是xaml:

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:Sym.VisualStudioExtension" x:Class="Sym.VisualStudioExtension.Engines.UtilitiesView"
         xmlns:engines="clr-namespace:Sym.VisualStudioExtension.Engines" 
         Background="{DynamicResource VsBrush.Window}"
         Foreground="{DynamicResource VsBrush.WindowText}"
         mc:Ignorable="d"
         local:ViewModelLocator.AutoWireViewModel="True"
         x:Name="MyToolWindow" Height="800" Width="400">

<UserControl.Resources>
    <DataTemplate DataType="{x:Type engines:CalcEngineViewModel}">
        <engines:CalcEngineView/>
    </DataTemplate>
    <DataTemplate DataType="{x:Type engines:TAEngineViewModel}">
        <engines:TAEngineView/>
    </DataTemplate>
</UserControl.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="207*" />
        <RowDefinition Height="593*"/>
    </Grid.RowDefinitions>
    <Grid x:Name="MainContent"
        Grid.Row="1" Grid.RowSpan="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <ContentControl Content="{Binding CurrentEngineViewModel}" Grid.RowSpan="2"/>
    </Grid>

</Grid>

并且用户做出一个决定当前EngineView模型的选择

以下是一个可能的CurrentEngineView模型的xaml:

<UserControl x:Class="Sym.VisualStudioExtension.Engines.CalcEngineView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"   
         xmlns:domain="clr-namespace:Sym.Engines.Calculation.Builder.Domain;assembly=Sym.Engines.Calculation" 
         xmlns:core="clr-namespace:Sym.Core.Domain;assembly=Sym.Core" 
         xmlns:helper="clr-namespace:Sym.VisualStudioExtension.Helper_Classes"
         xmlns:local="clr-namespace:Sym.VisualStudioExtension"           
         local:ViewModelLocator.AutoWireViewModel="True"
         mc:Ignorable="d"             
         d:DesignHeight="800" d:DesignWidth="400">
<UserControl.Resources>
    <ContextMenu>
        ...
    </ContextMenu>
</UserControl.Resources>
<Grid Margin="-20,-20,-20,-20">
    <Grid.RowDefinitions>
        <RowDefinition Height="8*"/>

    </Grid.RowDefinitions>
    <TabControl x:Name="tabControl" HorizontalAlignment="Left" Height="617" Margin="20,54,0,0" VerticalAlignment="Top" Width="357">
        <TabItem Header="Calculation Files">
            <ListBox Name="CalcFilesListBox" 
                     Margin="20" ItemsSource="{Binding CalcFilesList}"                           
                     ContextMenu="{StaticResource NewEditDeleteContextMenu}"
                     Tag="{x:Type core:CalcFile}">
                ...
            </ListBox>                
        </TabItem>
        <TabItem Header="Template Files" Height="22" VerticalAlignment="Top">
            <TreeView ItemsSource="{Binding TemplateFamilyList}" 
                      Margin="20"
                      ContextMenu="{StaticResource NewEditDeleteContextMenu}"
                      Tag="{x:Type domain:TemplateParameter}">
                <TreeView.Resources>
                    ...
                </TreeView.Resources>
            </TreeView>
        </TabItem>
        <TabItem Header="Advanced Calc Files">
            <ListBox Margin="20" 
                     ItemsSource="{Binding AdvancedCalcFilesList}"
                     ContextMenu="{StaticResource NewEditDeleteContextMenu}"
                     Tag="{x:Type domain:TemplateCalculation}">
                ...
            </ListBox>
        </TabItem>
    </TabControl>
    <Label x:Name="label" Content="{Binding Path=Title}" HorizontalAlignment="Left" Height="27" Margin="10,22,0,0" VerticalAlignment="Top" Width="367"/>
</Grid>

...
...
...
...

在我看来,在ToolWindowPane上有一个网格,网格上有另一个网格,网格上有一个Tabcontrol。从外观上看,某些控件似乎不调整大小。那么这是否意味着TabControl即使在网格上也无法调整大小

当用户调整ToolWindowPane的大小时,内容不会调整大小。 在这种情况下,如何实现正确的大小调整


要使页面在不同分辨率/设备间销售,必须确保未指定固定宽度/高度属性;相反,我们可以在百分比(%)中使用它们,这将使控件根据可用视口进行缩放。尝试用百分比替换控件的所有固定高度和宽度属性


如果由于某些原因,控件受到挤压,您可以尝试向控件提供minHeight和minWidth,如本文所述

问题的根源在于设置了显式的宽度和高度值,从而阻止控件装入其容器。

您已经有了
<代码>左侧防止选项卡控件水平调整大小。请尝试“拉伸”。。。。您还需要从TabControl中删除
Width=“357”
,因为这也会阻止TabControl水平调整大小。在上面的建议之后,不幸的是,我仍然存在我原来帖子中屏幕截图所示的情况。当ToolWindow变小时,TabControl会被切断。我认为问题的根源在于您在任何地方都使用固定的宽度和高度。应将其移除,以便与容器相适应。首先是实用程序视图,这样它就可以放在SymCalculationUtilities窗口中,然后是TabControl,这样它就可以放在网格中。@JoelLucsy我相信是这样的。我建议将此作为答案发布