我有一个TextBox和一个标签.单击按钮后,我执行以下代码:
label1.Content = textbox1.Text;
Run Code Online (Sandbox Code Playgroud)
我的问题是,如何启用标签的文本换行?可能有太多文本要显示在一行上,如果是这种情况,我希望它自动换行到多行.
我的TextBoxwpf应用程序中有2个es,一个用于用户名,另一个用于密码,两者都有FontSize=20,但文本显示如下:

我怎样才能解决这个问题?
XAML:
<TextBox Grid.Row="1" Grid.Column="1" Height="40" BorderThickness="1" BorderBrush="#FFD5D5D5" FontSize="36" Text="test" />
<PasswordBox Grid.Row="3" Grid.Column="1" Height="40" BorderThickness="1" BorderBrush="#FFD5D5D5" FontSize="36" Password="test" />
Run Code Online (Sandbox Code Playgroud) 假设我在Label旁边有一个简单的TextBox:
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Margin="3">MyLabel</Label>
<TextBox Margin="3" Width="100">MyText</TextBox>
</StackPanel>
...
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
这产生以下结果:
如您所见,MyLabel和MyText的基线未对齐,看起来很难看.当然,我可以开始玩边缘直到它们匹配,但由于这是一个常见的要求,我确信WPF提供了一个更简单,更优雅的解决方案,我还没有找到...
我试图将文本居中在TextBlock和PasswordBox中.
在TextBlock中,我使用TextAlignment属性水平居中文本,但它仍然靠近顶部.如何垂直居中?
而在PasswordBox中没有与文本对齐关联的属性,如何实现这一点?
我正在尝试使用VerticalContentAlignment属性垂直居中TextBox 的内容,但它似乎根本没有任何效果.文字保持在顶部.谁能告诉我怎么做?
这是我的代码:
<TextBox Grid.Column="1"
Grid.Row="0"
Width="200"
Height="28"
VerticalAlignment="Center"
VerticalContentAlignment="Center" />
Run Code Online (Sandbox Code Playgroud) 我DataGrid在C#中创建一个(来自代码隐藏/不是XAML),但无论我尝试什么,我都无法将文本垂直居中在数据单元格中:
![]](https://i.stack.imgur.com/hu7Rlm.png)
我开始时:
var CellStyle = new Style(typeof(DataGridCell)) {
Setters = {
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
}
};
Run Code Online (Sandbox Code Playgroud)
哪个正确地定位单元格并使文本水平居中(根据上面的屏幕截图).
试图垂直居中文本,我知道a TextBlock不支持垂直内容对齐,只支持父元素中自己的垂直对齐.
根据这个问题(WPF TextBlock中的文本垂直对齐)我试图使用Padding以下方法伪造它:
var CellStyle = new Style(typeof(DataGridCell)) {
Setters = {
new Setter(TextBlock.PaddingProperty, new Thickness(5)),
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center)
}
};
Run Code Online (Sandbox Code Playgroud)
这没有任何区别.然后我尝试了这个:
var CellStyle = new Style(typeof(DataGridCell)) {
Setters = {
new Setter(DataGridCell.VerticalContentAlignmentProperty, VerticalAlignment.Center),
new Setter(TextBlock.TextAlignmentProperty, TextAlignment.Center),
new Setter(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center)
}
};
Run Code Online (Sandbox Code Playgroud)
结果导致:

new Setter(DataGridCell.HeightProperty, 50d),在屏幕截图#1中添加结果.
如何在数据单元格中垂直居中显示文本?