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>
I’ve tried adding the elements inside into a Grid and assigning the ColumnDefinitions 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.
UPDATE:
<DockPanel LastChildFill="True">
Doesn’t work, It will not fill the ToolBar element like it would a normal element.
Further investigation showed that in order to do this I need to set the width of a
Gridwithin theToolBar, or as Chris Nicol said, aDockPanelwithin theToolBardynamically to that of the width of theToolbarusingRelativeSource.However, that did not feel like a clean solution. It is quite complicated to get the
Toolbarto update correctly on resizing. So instead I found somewhat of a hack that looks, and operates cleaner by adding an external Grid.Since all of my elements are on a Grid, I can place my
ComboBoxon top of theToolBarby assigning it’sGrid.Rowto the same row as the toolbar. After setting myMarginsto pull theComboBoxover 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.