views:

478

answers:

2

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.

+2  A: 

Have you tried using a DockPanel that fills the toolbar, then you can dock the ComboBox to the right.

Remember that with a dockpanel the sequence you put the items in is very important.

HTH

Chris Nicol
How would you fill the toolbar with a DockPanel? I tried and can only do it dynamically.. Unless there is something I am unaware of, this is the problem with the ToolBar. Which is why this won't work for me.
jsmith
just super busy with work at the moment, I'll post a solution this weekend that should feel a little cleaner.
Chris Nicol
+1  A: 

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"/>

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.

jsmith
Actually I find this a brilliant (and very neat) solution. I've been plagued by this issue for so long. Thanks!
pongba