如何在 WPF Expander Header 中对齐 TexBlock
我在 Expander 标头中有多个 Texblock。 我希望这些 TextBlock 左对齐、右对齐和居中对齐。
我怎样才能做到这一点?
请帮助...
谢谢
Sharath
红色文本应左对齐,蓝色文本应居中对齐,绿色文本应右对齐
请复制 XAMLPad.Exe 中的代码
I have Multiple Texblock in Expander Header. I want these TextBlocks aligned to Left, Right and Center.
How can i achieve this?
Please Help...
Thanks
Sharath
The Text in Red should be left aligned, the one in blue should be center aligned and the green should be right aligned
Please copy the code in XAMLPad.Exe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
之所以不能简单地使用带有 TextBlock 的 Grid 或 Dock 面板来控制对齐方式,是因为 Expander 的默认模板左对齐标题内容而不是允许其拉伸。
有两种方法可以解决这个问题。 您可以指定扩展器标题内容的宽度,也可以为扩展器定义新模板。
第二种选择可能是最好的,但也是更难实施的一种。 如果您有 Expression Blend,则可以右键单击“对象和时间轴”树中的 Expander 元素,然后选择“编辑控制部件 ->” 编辑副本...完成后,您还需要修改 HeaderSite 的模板(不过这次您可以选择编辑模板而不是创建副本)。 一旦你在那里,你几乎可以将其设置为拉伸以进行水平对齐,并在右侧添加一点边距,然后就完成了。
如果您没有 Expression Blend,请更新问题以指定这一点,我将发布一些示例代码。 我在这里没有这样做的主要原因是模板大约有 250 行代码,我不想无缘无故地产生大量噪音。
鉴于您给出的答复,我猜您没有 Blend。 如果将以下 XAML 放入 XAML Pad,您应该会看到一个 Expander 的示例,它将使标头内容能够跨越 Expander 的整个宽度。 您很可能希望将页面的资源移动到单独的资源字典中,以便可以通过应用程序重用它。
The reason that you can't simply use a Grid or Dock panel with the TextBlocks in to control the alignment is because the default template of Expander left aligns the header content instead of allowing it to stretch.
There are two ways around this. You can either specify a width for the content of the Expander's header or you can define a new template for the expander.
The second option is probably the best, but it is also the harder one to implement. If you have Expression Blend then you can right click the Expander element in the Objects and Timeline tree and select Edit Control Parts -> Edit a copy... Once you have done that you will need to modify the HeaderSite's template as well (you can select Edit Template this time though instead of creating a copy). Once your in there you can pretty much just set it to Stretch for horizontal alignment and add a bit of margin to the right and your done.
If you don't have Expression Blend update the question to specify that and I will post some sample code. The main reason I haven't done that here is that the template is about 250 lines of code and I didn't want to generate alot of noise for no reason.
Given the reply that you gave I am guessing you don't have Blend. If you put the following XAML into XAML Pad you should see an example of an Expander which will enable the header content to span the full width of the Expander. You will most likely want to move the resources for the page into a seperate resource dictionary so you can reuse it throught the application.
请复制 XAMLPad.Exe 中的代码
红色文本应左对齐,蓝色文本应居中对齐,绿色文本应右对齐
请回复。
谢谢
Please copy the code in XAMLPad.Exe
The Text in Red should be left aligned, the one in blue should be center aligned and the green should be right aligned
Please reply.
Thanks