是否有可能使一个WPF工具栏中的元素有正确的的HorizontalAlignment?
Is it possible to make the elements within a WPF toolbar have a HorizontalAlignment of Right?
<ToolBar Height="38" VerticalAlignment="Top" Grid.Row="1">
<Button HorizontalAlignment="Left" Width="50" VerticalAlignment="Stretch"/>
<Button HorizontalAlignment="Left" Width="50" VerticalAlignment="Stretch"/>
<ComboBox Width="120" HorizontalAlignment="Right"/>
</ToolBar>
我已经试过里面添加元素融入到一个网格,并分配 ColumnDefinition
s到左/右为好。我也尝试了的StackPanel
。不管我怎么努力,我似乎无法得到组合框是锚定右侧工具栏的。
I've tried adding the elements inside into a Grid and assigning the ColumnDefinition
s to Left/Right as well. I have also tried a StackPanel
. No matter what I try I can't seem to get the ComboBox to be "anchored" on the right side of the Toolbar.
更新:
<DockPanel LastChildFill="True">
不行的,它不会填补像它的工具栏元素将一个正常的元素。
Doesn't work, It will not fill the ToolBar element like it would a normal element.
进一步的调查表明,为了做到这一点,我需要设置一个电网
的宽度内工具栏
,或者克里斯·尼科尔说,一个 DockPanel中
在工具栏
动态到的宽度工具栏
使用的RelativeSource
。
Further investigation showed that in order to do this I need to set the width of a Grid
within the ToolBar
, or as Chris Nicol said, a DockPanel
within the ToolBar
dynamically to that of the width of the Toolbar
using RelativeSource
.
不过,这并不觉得自己是一个干净的解决方案。这是相当复杂的,以获得工具栏
来调整上正确地更新。所以不是我发现有些黑客看起来,其操作更清洁。
However, this does not feel like a clean solution. It is quite complicated to get the Toolbar
to update correctly on resizing. So instead I found somewhat of a hack that looks, and operates cleaner.
<ToolBar Height="38" VerticalAlignment="Top" Grid.Row="1">
<Button HorizontalAlignment="Left" Width="50" VerticalAlignment="Stretch"/>
<Button HorizontalAlignment="Left" Width="50" VerticalAlignment="Stretch"/>
</ToolBar>
<ComboBox Margin="0,0,15,0" Width="120" HorizontalAlignment="Right" Grid.Row="1"/>
由于我所有的元素都在一个网格,我可以把我的组合框
在工具栏
的顶端,分配给它的 Grid.Row
至同一行的工具栏。设置完成后我的页边距
拉组合框
在稍不干扰的外观,它需要与没有错误操作。由于唯一的其他方式,我发现这样做是动态地设置一个DockPanel中/格的宽度属性,其实我觉得这是更清洁更有效的方式来做到这一点。
Since all of my elements are on a Grid, I can place my ComboBox
on top of the ToolBar
by assigning it's Grid.Row
to the same row as the toolbar. After setting my Margins
to pull the ComboBox
over slightly as not to interfere with looks, it operates as needed with no bugs. Since the only other way I found to do this was setting a DockPanel/Grid's Width property dynamically, I actually feel like this is the cleaner more efficient way to do it.