【问题标题】:How to adjust width according to the space available for wpf toolbartray如何根据 wpf 工具栏的可用空间调整宽度
【发布时间】:2011-12-14 09:33:02
【问题描述】:

我在 WPF ToolbarTray 控件中使用了大约 10-15 个控件,现在的问题是,当我更改分辨率时,其中存在的控件在最大化窗口的情况下没有根据我的需要进行调整。有关更多详细信息,我附上了下面的屏幕截图。

最初出现的工具栏托盘控件:

工具栏托盘需要像这样出现(我正在尝试):

谁能告诉我如何自定义或如何实现第二个图像,如 ToolbarTray。感谢任何帮助。

提前致谢,

更新:

<ToolBarTray Background="White">
    <ToolBar>

        <Button
        Width="50" Content="hi"/>
        <Button
        Width="100" Content="bye"/>
        <Button
        Content="welcome"/>
        <Button
        Width="20"/>
        <Button Content="Welcome"/>
        <Separator />
        <Button
        ToolBar.OverflowMode="Always" Content="save" />
        <Button
        ToolBar.OverflowMode="Always" Content="open" />
        <Button
        ToolBar.OverflowMode="AsNeeded" Content="bmp" />
    </ToolBar>
    <ToolBar HorizontalAlignment="Right">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
            <Button Content="New Hi1"/>
            <Button Content="New Hi2"/>
            <Button Content="New Hi3"/>
        </StackPanel>
    </ToolBar>
</ToolBarTray>

【问题讨论】:

  • 能否请您发布您的 XAML?
  • @Orchestrator:我更新了 XAML 代码

标签: c# wpf user-controls controltemplate


【解决方案1】:

据我所知,这是不可能的,因为 ToolbarTray 的布局选项非常有限。

您可以做的是将工具栏包装在 DockPanel 中以进行布局。但是您缺少一些东西,例如通过拖放更改工具栏位置。

  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="50"></RowDefinition>
      <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
      <DockPanel Grid.Row="0" Background="White">
        <ToolBar DockPanel.Dock="Left">
         ....
      </ToolBar>
      <ToolBar DockPanel.Dock="Right" HorizontalAlignment="Right" HorizontalContentAlignment="Stretch">
        ...
      </ToolBar>
    </DockPanel>

【讨论】:

  • 我们不能在 ToolBarTray 中使用这个 DockPanel,在那个 DockPanel 中同时使用 ToolBar 吗?
  • 不,ToolbarTray 只允许 ToolBars 作为内容
  • 是的,但我想修复第一个工具栏而不是第二个。虽然我将从右侧拖动窗口以调整大小,但第二个工具栏将根据可用空间运行。
【解决方案2】:

我找到了我的问题的解决方案,现在我在 ToolBar 控件中使用了一个 Grid,并调整了 Grid(toolbarGrid) 的一列的大小,以使我的控件如我的第二张图片中所示。

代码:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="0"/>
    <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

<Stackpanel
    Grid.Column ="0">

    <Button Width="50" Content="hi"/>
        <Button Width="100" Content="bye"/>
        <Button Content="welcome"/>
        <Button Width="20"/>
        <Button Content="Welcome"/>
        <Separator />
        <Button ToolBar.OverflowMode="Always" Content="save" />
        <Button ToolBar.OverflowMode="Always" Content="open" />
        <Button ToolBar.OverflowMode="AsNeeded" Content="bmp" />
</StackPanel>

    <ToolBarTray x:Name ="toolBarTray" Grid.Column ="1" HorizontalAlignment ="Right" IsLocked="True">

        <!--First Toolbar for the controls from Print to Zoom Control-->
        <ToolBar x:Name ="topToolBar" FocusVisualStyle="{x:Null}">
            <Grid x:Name="toolbarGrid">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="0"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Button Content="New Hi1"/>
                <Button Content="New Hi2"/>
                <Button Content="New Hi3"/>
            </Grid>
        </ToolBar>
    </ToolBarTray>
</Grid>

【讨论】:

  • 在 Wondow.SizeChanged 事件中,我正在调整 ToolBarGrid 列的宽度,但每次调整窗口大小时我都没有得到准确的值。有谁知道如何解决这个问题?
【解决方案3】:

简单的答案不会是在应用程序的顶部“行”中绘制灰色背景??

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    相关资源
    最近更新 更多