C#WPF调整大小控件在网格行填满所有宽度

weixin_38078582 2019-09-12 03:16:07

我试图让我的组合框拉伸使用所有的空余空间中展开的窗口。如果窗口的宽度缩小,ToolBarTray将会崩溃。 我使用的网格指定为分别* & 300 2列与宽度。 如何制作组合框来填充行中的所有可用空间? <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>






...全文
2016 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38108228 2019-09-12
  • 打赏
  • 举报
回复

只要改变你的列宽的定义: <Grid DockPanel.Dock="Top"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> 默认情况下,组合框已设置为Stretch水平和垂直对齐。 可能是第一列,你可以把一个硬编码的大小。 但是,如果你的GUI需要有更多的空间来还要好,把周围的ToolbarTray一些利润率和ComboBox 问候
weixin_38108704 2019-09-12
  • 打赏
  • 举报
回复

使用的Horizo​​ntalAlignment = “STRECH” 请参见下面的代码 <ComboBox Grid.Column="1" MinWidth="200" HorizontalAlignment="Stretch"></ComboBox>
weixin_38108998 2019-09-12
  • 打赏
  • 举报
回复

好吧,如果你希望工具栏完全崩溃了,我找到的唯一解决方案是编写代码一点点。 仅在XAML中进行布局会更加优雅,但有时​​C#代码是唯一的方法。 摆脱电网: 处理画布的大小调整: 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 希望我理解你希望它可以帮助,视

473

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧