WPF - Canvas 内的中心控件(标签)

发布于 2024-10-07 20:11:31 字数 629 浏览 2 评论 0原文

您好,

我正在尝试修复 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乞讨 2024-10-14 20:11:31

您不应该为此使用 Canvas。对齐属性和边距对画布中布局的元素没有影响。你应该使用网格。 Canvas 对于大多数布局场景来说并不是特别有用。

<Grid 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" 
           HorizontalAlignment="Center" Margin="0,5,0,0" 
           Name="labelPlayer1Name" VerticalAlignment="Center" />
</Grid>

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.

<Grid 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" 
           HorizontalAlignment="Center" Margin="0,5,0,0" 
           Name="labelPlayer1Name" VerticalAlignment="Center" />
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文