C# WPF工具栏控制溢出量

C# WPF工具栏控制溢出量,c#,wpf,C#,Wpf,我正在编写一个具有多个工具栏控件的应用程序。工具栏之间还有其他UI元素,因此我无法使用工具栏管理器。以下是UI层次结构: <UserControl> <DockPanel> <StackPanel DockPanel.Dock="Top"> <ToolbarTray> <Toolbar/> <ToolbarTray/> <S

我正在编写一个具有多个工具栏控件的应用程序。工具栏之间还有其他UI元素,因此我无法使用工具栏管理器。以下是UI层次结构:

<UserControl>
     <DockPanel>
           <StackPanel DockPanel.Dock="Top">
                <ToolbarTray> <Toolbar/> <ToolbarTray/>
                <Some Other UI Stuff, separators, text, etc/>
           <StackPanel/>

           <StackPanel DockPanel.Dock="Top">
                <ToolbarTray> <Toolbar/> <ToolbarTray/>
                <Some Other UI Stuff, separators, text, etc/>
           <StackPanel/>



           <!-- as the above toolbars expand in size, these two are pushed offscreen>
           <StackPanel DockPanel.Dock="Bottom">
                <Some Other UI Stuff, separators, text, etc/>
           <StackPanel/>
    <DockPanel/>
<UserControl/>


您应该将优先级最高的区域放在dockpanel的第一位

您可以使用以下非常原始的设置来测试这一点(放置在WPF窗口中并调整高度,观察控件展开/收缩的顺序):

您当前的方式:

<DockPanel>
    <Rectangle DockPanel.Dock="Top" Fill="Green" Height="100"/>
    <Rectangle DockPanel.Dock="Bottom" Fill="Red" Height="100"/>
    <!-- Last one fills -->
    <Rectangle Fill="Orange"/>
</DockPanel>
还可以为原始视图和旋转视图定义不同的数据模板。这完全取决于相关控制的要求和复杂性

另一个备选方案:让我们实际旋转视图,包括其内容。。。如果父容器未旋转,这可能会有所帮助:

private bool isRotated = false;
private void Button_Click(object sender, RoutedEventArgs e)
{
    if (!isRotated)
    {
        myGrid.LayoutTransform = new RotateTransform(90, myGrid.ActualWidth / 2, myGrid.ActualHeight / 2);
    }
    else
    {
        myGrid.LayoutTransform = Transform.Identity;
    }
    isRotated = !isRotated;
}

使用带有行定义的
Grid
s而不是前两个
StackPanel
s是否有帮助
StackPanel
始终使用所有可用空间。由于应用程序的行为,我必须使用StackPanel。它从垂直方向旋转到水平方向。您可以将
行定义
s更改为
列定义
s,并相应地更新子级的
网格.行
网格.列
属性。是否适应方向?是的,我可以,但是考虑到应用程序的预期UI设计,使用stackpanels要容易得多。我只是想知道为什么允许它们完全超出usercontrol的边界……另一种方法是将
StackPanel
Height
属性绑定到其他父控件的
Height
。但是,我不确定这是否是你想要的。你能提供一些截图吗?谢谢,但这并不能解决我的问题。在您的示例中,橙色矩形是唯一大小发生变化的矩形。我面临的问题是,橙色和绿色块的大小都应该改变,而红色块保持不变。注意,我有两个应该溢出的工具栏控件,底部有一个固定控件(红色块)。这就是困难所在——将两个StackPanel中的工具栏控件一起缩放。第一个stackpanel的比例为30%,第二个stackpanel的比例为20%,尽管这个比例对解决问题并不重要。@AlexRosenfeld请参见我对基于比例的尺寸模型的编辑。对于几乎所有一致且清晰的布局规范,都有一个解决方案,而不一致或不完整的要求在某一点上会失败-在这种情况下,问题在于固定大小和20%+30%的组合,对于大多数窗口大小,这两种组合不会达到100%。所以你的要求仍然不准确。抱歉。20%+30%用于带有工具栏控件的两个stackpanel,剩余部分留给最后一个stackpanel填充。两个工具栏控件中的第二个和最终stackpanel之间可能有空白
<DockPanel KeyboardNavigation.TabNavigation="Local">
    <Rectangle DockPanel.Dock="Bottom" Fill="Red" Height="100" KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.TabIndex="3"/>
    <Rectangle DockPanel.Dock="Top" Fill="Green" Height="100" KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.TabIndex="1"/>
    <!-- Last one fills -->
    <Rectangle Fill="Orange" KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.TabIndex="2"/>
</DockPanel>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Rectangle Grid.Row="0" Fill="Green"/>
    <Rectangle Grid.Row="1" Fill="Orange"/>
    <Rectangle Grid.Row="2" Fill="Red" Height="100"/>
</Grid>
<Grid x:Name="myGrid" Margin="0,0,0,40">
    <Grid.RowDefinitions>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="2*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Rectangle Grid.Row="0" Grid.Column="0" Fill="Green"/>
    <Rectangle Grid.Row="1" Grid.Column="0" Fill="Orange"/>
    <Rectangle Grid.Row="2" Grid.Column="0" Fill="Red" MinHeight="100" MinWidth="100"/>
</Grid>
<Button VerticalAlignment="Bottom" HorizontalAlignment="Left" Margin="10" Content="Change Rotation" Click="Button_Click"/>
private void Button_Click(object sender, RoutedEventArgs e)
{
    var rd = myGrid.RowDefinitions.ToList();
    var cd = myGrid.ColumnDefinitions.ToList();
    myGrid.RowDefinitions.Clear();
    myGrid.ColumnDefinitions.Clear();
    foreach (var item in cd)
    {
        myGrid.RowDefinitions.Add(new RowDefinition() { Height = item.Width });
    }
    foreach (var item in rd)
    {
        myGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = item.Height });
    }
    foreach (UIElement child in myGrid.Children)
    {
        var r = child.GetValue(Grid.RowProperty);
        child.SetValue(Grid.RowProperty, child.GetValue(Grid.ColumnProperty));
        child.SetValue(Grid.ColumnProperty, r);
    }
}
private bool isRotated = false;
private void Button_Click(object sender, RoutedEventArgs e)
{
    if (!isRotated)
    {
        myGrid.LayoutTransform = new RotateTransform(90, myGrid.ActualWidth / 2, myGrid.ActualHeight / 2);
    }
    else
    {
        myGrid.LayoutTransform = Transform.Identity;
    }
    isRotated = !isRotated;
}