WPF - Canvas 内的中心控件(标签)
您好,
我正在尝试修复 WPF 应用程序中控件的位置。在我的应用程序中,我有几个标签,每个标签都位于不同的画布中。我试图将标签置于画布的中心。我目前的一个标签的代码如下:
<Canvas Height="42" HorizontalAlignment="Left" Margin="0,1,0,0" Name="canvasPlayer1" VerticalAlignment="Top" Width="172" >
<Label Content="" Foreground="White" FontSize="15" FontFamily="Eurostile LT ExtendedTwo" Height="Auto" HorizontalAlignment="Center" Margin="0,5,0,0" Name="labelPlayer1Name" VerticalAlignment="Center" Width="Auto" />
</Canvas>
现在对于初学者来说,问题是它根本不使文本居中,它停留在原始位置。我想要实现的是将标签的内容在该画布中居中,但它不能超过画布的宽度。
欢迎有帮助的建议!如果您对我解决此问题的思维模式有任何意见,请也通知我!
先感谢您
Greetings
I'm trying to fix the positioning of controls in my WPF Application. In my application I have several Labels which each are in a different Canvas. I'm trying to center the label within the canvas. The code I currently have for one Label is the following:
<Canvas Height="42" HorizontalAlignment="Left" Margin="0,1,0,0" Name="canvasPlayer1" VerticalAlignment="Top" Width="172" >
<Label Content="" Foreground="White" FontSize="15" FontFamily="Eurostile LT ExtendedTwo" Height="Auto" HorizontalAlignment="Center" Margin="0,5,0,0" Name="labelPlayer1Name" VerticalAlignment="Center" Width="Auto" />
</Canvas>
Now for starters the problem with this is that it does not center the text at all, it stays at the original position. What I'm trying to achieve is to center the content of the label in that canvas BUT it cannot exceed the width of the canvas.
Helpful suggestions are welcomed! If you have any comments regarding my thinking pattern to solve this issue please do notify me as well!
Thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该为此使用 Canvas。对齐属性和边距对画布中布局的元素没有影响。你应该使用网格。 Canvas 对于大多数布局场景来说并不是特别有用。
You shouldn't use Canvas for this. Alignment properties and margins will have no effect on elements laid out in a Canvas. What you should use instead is a Grid. Canvas is not particularly useful for most layout scenarios.