2015-10-13 1104 views
0

我试图让我的组合框拉伸使用所有的空余空间中展开的窗口。如果窗口的宽度缩小,ToolBarTray将会崩溃。C#WPF调整大小控件在网格行填满所有宽度

我使用的网格指定为分别* & 300 2列与宽度。 如何制作组合框来填充行中的所有可用空间?

Expanded window Collapsed window

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="50" /> 
    </Grid.RowDefinitions> 

    <DockPanel LastChildFill="True" Grid.Row="0"> 
     <Grid DockPanel.Dock="Top"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="*" /> 
       <ColumnDefinition Width="300" /> 
      </Grid.ColumnDefinitions> 

      <ToolBarTray DockPanel.Dock="Left"> 
       <ToolBar Name="Standard"> 
        <Button Height="32"> 
         <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right"> 
          <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" /> 
         </StackPanel> 
        </Button> 

        <Button Height="32"> 
         <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right"> 
          <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" /> 
         </StackPanel> 
        </Button> 

        <Button Height="32"> 
         <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right"> 
          <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" /> 
         </StackPanel> 
        </Button> 
       </ToolBar> 

       <ToolBar Name="Standard2"> 
        <Button Height="32"> 
         <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right"> 
          <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" /> 
         </StackPanel> 
        </Button> 

        <Button Height="32"> 
         <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right"> 
          <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" /> 
         </StackPanel> 
        </Button> 

        <Button Height="32"> 
         <StackPanel Orientation="Horizontal" Margin="5,0,5,0" HorizontalAlignment="Right"> 
          <Image Height="24" Width="24" HorizontalAlignment="Center" Source="Images/document.png" /> 
         </StackPanel> 
        </Button> 
       </ToolBar> 
      </ToolBarTray> 

      <ComboBox Grid.Column="1" MinWidth="200" ></ComboBox> 
     </Grid> 
    </DockPanel> 
</Grid> 

回答

0

只要改变你的列宽的定义:

<Grid DockPanel.Dock="Top"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto" /> 
      <ColumnDefinition Width="*" /> 
     </Grid.ColumnDefinitions> 

默认情况下,组合框已设置为Stretch水平和垂直对齐。

可能是第一列,你可以把一个硬编码的大小。

但是,如果你的GUI需要有更多的空间来还要好,把周围的ToolbarTray一些利润率和ComboBox

问候

+0

使用'WIDTH =“Auto'防止ToolBarTray从正确折叠。 最终,我所做的就是指定'WIDTH = “*” MaxWidth = “400”'和'WIDTH = “*” MinWidth = “500”'关于相应列。 它是一种解决方法,我必须找到ToolBarTray的绝对宽度,例如400并设置为MaxWidth。自从向ToolBarTray添加新项目以来,不是最好的方式意味着再次找到绝对宽度。希望有更好的解决方案。 –

+0

我试了一下你的代码,我的解决方案,它似乎很好。我可以调整工具栏大小,右边没有额外的空间。什么是崩溃的问题?这里是我的解决方案http://1drv.ms/1LInfDa比较 –

+0

[链接](https://onedrive.live.com/redir?resid=2BD0338CF8FC5F36!42820&authkey=!ACAUjgfs1ZBLmrs&ithint = file%2czip) 这是我的示例解决方案。随着窗口宽度的减小,ComboBox会推动ToolBarTray,折叠按钮。 –

1

使用的Horizo​​ntalAlignment = “STRECH”

请参见下面的代码

<ComboBox Grid.Column="1" MinWidth="200" HorizontalAlignment="Stretch"></ComboBox> 
1

好吧,如果你希望工具栏完全崩溃了,我找到的唯一解决方案是编写代码一点点。 仅在XAML中进行布局会更加优雅,但有时​​C#代码是唯一的方法。

  1. 摆脱电网:

  2. 处理画布的大小调整:

    private void ToolbarSizeChanged(object sender, SizeChangedEventArgs e) 
    { 
        const double COMBO_MIN_DESIRED_WIDTH = 300.0; 
        Size newSize = e.NewSize; 
    
        Size sizeToolbarTray = new Size(Double.PositiveInfinity, newSize.Height); 
        // measure to update DesiredSize 
        toolBarTray.Measure(sizeToolbarTray); 
    
        Rect toolBarTrayRect = new Rect(0, 0, 0, newSize.Height); 
        Rect comboRect =new Rect(0,0,0,newSize.Height); 
    
        // 3 cases : 
    
        // 1) Much space is available 
        if (newSize.Width > toolBarTray.DesiredSize.Width + COMBO_MIN_DESIRED_WIDTH) 
        { 
         toolBarTrayRect.Width = toolBarTray.DesiredSize.Width; 
         comboRect.X = toolBarTrayRect.Width; 
         comboRect.Width = newSize.Width -toolBarTrayRect.Width; 
    
        } 
        // 2) Space to show Combo, but not totally toolbar 
        else if (newSize.Width > COMBO_MIN_DESIRED_WIDTH) 
        { 
         toolBarTrayRect.Width = newSize.Width - COMBO_MIN_DESIRED_WIDTH; 
         comboRect.X = toolBarTrayRect.Width; 
         comboRect.Width = COMBO_MIN_DESIRED_WIDTH; 
        } 
        // 3) Not enough space to show toolbar 
        else 
        { 
         toolBarTrayRect.Width = 0; 
         comboRect.Width = newSize.Width; 
        } 
        // Layout the two components : 
        toolBarTray.Arrange(toolBarTrayRect); 
        combobox.Arrange(comboRect); 
    } 
    

这里到workiing解决方案的链接,让你CANN检查它的你想要什么:
http://1drv.ms/1MySnde

希望我理解你希望它可以帮助,视