在WPF中调整ToolWindow和内容的大小

时间:2018-06-30 10:40:09

标签: c# wpf

我们有一个VS扩展程序。用户使用ToolWindowPane与扩展进行交互。 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();

    }

}

因此,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>

,然后由用户做出确定CurrentEngineViewModel的选择。

以下是可能的CurrentEngineViewModels的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上有一个网格,在该网格上有另一个网格,在该网格上有一个Tab控件。从this post起,似乎某些控件没有调整大小。那是否意味着TabControl即使在网格上也无法调整大小?

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

enter image description here

2 个答案:

答案 0 :(得分:1)

要使页面在各种分辨率/设备上均可销售,则必须确保未指定固定的width / height属性。相反,我们可以按百分比(%)使用它们,这将使控件根据可用的视口缩放。尝试用百分比替换控件的所有固定高度和宽度属性。

如果出于某些原因,控件受到挤压,您可以尝试按此处https://www.w3schools.com/cssref/pr_dim_min-height.asp

中的说明向控件提供minHeight和minWidth。

答案 1 :(得分:1)

问题的根源是正在设置显式的Width和Height值,并防止控件适合其容器。