如何在工具栏中创建项目填充WPF中的所有可用空间

时间:2022-09-25 07:45:27

I'm trying to make an item on ToolBar (specifically a Label, TextBlock, or a TextBox) That will fill all available horizontal space. I've gotten the ToolBar itself to stretch out by taking it out of its ToolBarTray, but I can't figure out how to make items stretch.

我正在尝试在ToolBar上创建一个项目(特别是Label,TextBlock或TextBox),它将填充所有可用的水平空间。我已经将ToolBar本身从它的ToolBarTray中取出来伸展,但是我无法弄清楚如何让物品伸展。

I tried setting Width to Percenatage or Star values, but it doesn't accept that. Setting Horizontal(Content)Alignment to Stretch in various places seems to have no effect either.

我尝试将Width设置为Percenatage或Star值,但它不接受。在各个地方设置水平(内容)对齐拉伸似乎也没有效果。

5 个解决方案

#1


12  

Unfortunately it looks like the default ControlTemplate for ToolBar doesn't use an ItemsPresenter, it uses a ToolBarPanel, so setting ToolBar.ItemsPanel won't have any effect.

不幸的是,看起来默认的ControlTemplate for ToolBar不使用ItemsPresenter,它使用ToolBarPanel,因此设置ToolBar.ItemsPanel不会有任何影响。

ToolBarPanel inherits from StackPanel. By default its Orientation is bound to the parent ToolBar.Orientation, but you can override this and set its Orientation to Vertical with a Style and this will allow items to stretch horizontally:

ToolBarPanel继承自StackPanel。默认情况下,其Orientation绑定到父ToolBar.Orientation,但您可以覆盖它并使用Style将其Orientation设置为Vertical,这将允许项水平拉伸:

<DockPanel>
    <ToolBar DockPanel.Dock="Top">
        <ToolBar.Resources>
            <Style TargetType="{x:Type ToolBarPanel}">
                <Setter Property="Orientation" Value="Vertical"/>
            </Style>
        </ToolBar.Resources>
        <ComboBox HorizontalAlignment="Stretch" SelectedIndex="0">
            <ComboBoxItem>A B C</ComboBoxItem>
            <ComboBoxItem>1 2 3</ComboBoxItem>
            <ComboBoxItem>Do Re Mi</ComboBoxItem>
        </ComboBox>
    </ToolBar>
    <Border Margin="10" BorderBrush="Yellow" BorderThickness="3"/>
</DockPanel>

You can then use a Grid or something in place of the ComboBox above if you want multiple items on a line.

然后,如果您想要一行上有多个项目,则可以使用Grid或其他代替上面的ComboBox。

#2


0  

Try putting a horizontal StackPanel in the ToolBar and then the element you want inside of that StackPanel.

尝试在ToolBar中放置一个水平StackPanel,然后在StackPanel中放置你想要的元素。

#3


0  

Have you tried wrapping your item in a Grid, not in a StackPanel?

您是否尝试将项目包装在网格中,而不是在StackPanel中?

#4


0  

You need a special custom panel like auto stretched stack panel, and replace the toolbarpanel. Check this out you can find one panel there http://blendables.com/products/productsLayoutmix.aspx

您需要一个特殊的自定义面板,如自动拉伸堆栈面板,并替换toolbarpanel。看看这个,你可以找到一个面板http://blendables.com/products/productsLayoutmix.aspx

#5


0  

I've had this same problem for a while, and there is very little help available online.

我有一段时间遇到同样的问题,网上提供的帮助很少。

Since it doesn't sound like you need all the extra functionality of a Toolbar (collapsible/movable trays), why not just use a Top-docked Grid, and tweak the background a little to make it look like a standard toolbar?

因为它听起来不像你需要工具栏的所有额外功能(可折叠/可移动托盘),为什么不只是使用顶部停靠网格,并稍微调整背景使其看起来像标准工具栏?

#1


12  

Unfortunately it looks like the default ControlTemplate for ToolBar doesn't use an ItemsPresenter, it uses a ToolBarPanel, so setting ToolBar.ItemsPanel won't have any effect.

不幸的是,看起来默认的ControlTemplate for ToolBar不使用ItemsPresenter,它使用ToolBarPanel,因此设置ToolBar.ItemsPanel不会有任何影响。

ToolBarPanel inherits from StackPanel. By default its Orientation is bound to the parent ToolBar.Orientation, but you can override this and set its Orientation to Vertical with a Style and this will allow items to stretch horizontally:

ToolBarPanel继承自StackPanel。默认情况下,其Orientation绑定到父ToolBar.Orientation,但您可以覆盖它并使用Style将其Orientation设置为Vertical,这将允许项水平拉伸:

<DockPanel>
    <ToolBar DockPanel.Dock="Top">
        <ToolBar.Resources>
            <Style TargetType="{x:Type ToolBarPanel}">
                <Setter Property="Orientation" Value="Vertical"/>
            </Style>
        </ToolBar.Resources>
        <ComboBox HorizontalAlignment="Stretch" SelectedIndex="0">
            <ComboBoxItem>A B C</ComboBoxItem>
            <ComboBoxItem>1 2 3</ComboBoxItem>
            <ComboBoxItem>Do Re Mi</ComboBoxItem>
        </ComboBox>
    </ToolBar>
    <Border Margin="10" BorderBrush="Yellow" BorderThickness="3"/>
</DockPanel>

You can then use a Grid or something in place of the ComboBox above if you want multiple items on a line.

然后,如果您想要一行上有多个项目,则可以使用Grid或其他代替上面的ComboBox。

#2


0  

Try putting a horizontal StackPanel in the ToolBar and then the element you want inside of that StackPanel.

尝试在ToolBar中放置一个水平StackPanel,然后在StackPanel中放置你想要的元素。

#3


0  

Have you tried wrapping your item in a Grid, not in a StackPanel?

您是否尝试将项目包装在网格中,而不是在StackPanel中?

#4


0  

You need a special custom panel like auto stretched stack panel, and replace the toolbarpanel. Check this out you can find one panel there http://blendables.com/products/productsLayoutmix.aspx

您需要一个特殊的自定义面板,如自动拉伸堆栈面板,并替换toolbarpanel。看看这个,你可以找到一个面板http://blendables.com/products/productsLayoutmix.aspx

#5


0  

I've had this same problem for a while, and there is very little help available online.

我有一段时间遇到同样的问题,网上提供的帮助很少。

Since it doesn't sound like you need all the extra functionality of a Toolbar (collapsible/movable trays), why not just use a Top-docked Grid, and tweak the background a little to make it look like a standard toolbar?

因为它听起来不像你需要工具栏的所有额外功能(可折叠/可移动托盘),为什么不只是使用顶部停靠网格,并稍微调整背景使其看起来像标准工具栏?