右对齐 wpf 扩展器标题中的项目
我想让扩展器标题中的一些文本左对齐,然后一些文本右对齐。 我已经找到了如何将标题扩展到容器的宽度,并且认为我可以简单地添加一个停靠面板并将第二个文本块设置为“停靠右侧”,但这似乎没有帮助。 有什么解决办法吗?
<Expander>
<Expander.Header>
<DockPanel
Width="{Binding
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type Expander}},
Path=ActualWidth}">
<TextBlock
Text="I am header text…"
Background="LightBlue"
/>
<TextBlock DockPanel.Dock="Right"
Text="I am header text…"
Background="Yellow"
/>
</DockPanel>
</Expander.Header>
</Expander>
I want to have some text in an expander header left aligned, then some text right aligned.
I have found how to expand the header to the width of the container, and thought I could simply add a dockpanel and set the second text block to Dock Right, but it doesn't seem to help. Any solutions?
<Expander>
<Expander.Header>
<DockPanel
Width="{Binding
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type Expander}},
Path=ActualWidth}">
<TextBlock
Text="I am header text…"
Background="LightBlue"
/>
<TextBlock DockPanel.Dock="Right"
Text="I am header text…"
Background="Yellow"
/>
</DockPanel>
</Expander.Header>
</Expander>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这对我有用。 这也会将箭头向右移动,因为它会恢复标题的顺序并重新恢复内容的顺序。
(来源)
This is what worked for me. This also moves the arrow to the right, because it's reverting the order of the header and re-reverting the order for the content.
(Source)
尝试将
TextAlignment
属性设置为 right,将HorizontalAlignment
属性设置为在TextBlock
上拉伸。 我想这应该有帮助。如果您使用颜色的目的不是演示目的,并且您确实希望整个元素右对齐,那么您可能需要考虑设置
DockPanel
的LastChildFill
属性> 为假。Try setting the
TextAlignment
property to right and theHorizontalAlignment
property to stretch on theTextBlock
s. That should help i think.If your use of color is for something other than demo purposes and you literally want the whole element to be right aligned then you might want to look at setting the
LastChildFill
property of theDockPanel
to false.不幸的是,这与默认扩展器模板的问题有关,该模板将标题的水平对齐设置为左侧而不是拉伸。 让它工作的最好方法是创建一个新的模板来正确设置它。 以下是更多信息的链接:
http://silverlight.net/forums/p /57142/145801.aspx#145801
它适用于 silverlight,但也适用于 wpf。 另一种方法是将上面的停靠面板宽度绑定到包含扩展器的元素的实际宽度。 这不是一个很好的解决方案,但它确实有效。 您需要为宽度创建一个值转换器。 这是一些代码:
在您的 xaml 中:
同样,这不是最好的解决方案,但它应该可以工作。 需要注意的一件事是,如果将偏移值设置得太小并将其绑定到扩展器的父级,则 Visual Studio 可能会挂起,因为父级的实际宽度将基于扩展器的宽度。
如果这个实现还不太清楚,请告诉我。 再次,我真的建议只使用扩展器的自定义模板。 您可以获得默认模板,只需稍加修改即可使其正常工作。 如果您愿意,我也可以发布该内容。
Unfortunately, this has to do with an issue with the default expander template, which sets the horizontal alignment of the header to left instead of stretch. The best way to get it working would be to create a new template that sets this correctly. Here's a link for more info:
http://silverlight.net/forums/p/57142/145801.aspx#145801
it's for silverlight, but applies to wpf as well. Another way to do this would be to bind your dockpanel width above to the actual width of the element containing the expander. This isn't a great solution but it works. You'll need to create a value converter for the width. Here's some code:
And in your xaml:
Again, this isn't the best solution, but it should work. One thing to note, if you set the offset value too small and bind it to a parent of your expander, you can get visual studio to hang since the actualwidth of the parent would be based on your width of the expander.
Let me know if this implementation isn't quite clear. Again, I would really recommend just using a custom template for the expander. You can get the default template and just slightly modify it to make it work. I can post that as well if you'd like.
我通过在 Expander.Header 部分中添加一个网格并将其宽度设置为其祖先并拉伸它来解决这个问题。 现在网格按预期工作。
I solved this problem by adding a grid into the Expander.Header section and set it's width to its ancestor and stretching it. Now the Grid works as expected.