WPF工具栏项目Horizo​​ntalAligment ="右" [英] WPF Toolbar Items HorizontalAligment="Right"

查看:196
本文介绍了WPF工具栏项目Horizo​​ntalAligment ="右"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使一个WPF工具栏中的元素有正确的的Horizo​​ntalAlignment?

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>

我已经试过里面添加元素融入到一个网格,并分配 Col​​umnDefinition s到左/右为好。我也尝试了的StackPanel 。不管我怎么努力,我似乎​​无法得到组合框是锚定右侧工具栏的。

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.

更新:

<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.

这篇关于WPF工具栏项目Horizo​​ntalAligment =&QUOT;右&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆