我有问题。我的WPF应用程序中的文本在第一个单词之后换行。奇怪的是,第一个创建的文本或我以相同方式创建的任何其他文本都不会发生这种情况。
我已经尝试将Wrapping设置为Wrap,No Wrap并删除该选项,但是什么也没有改变。
这里是我的代码:
public partial class ConfigPage : Page
{
int hohe = 50;
public ConfigPage()
{
InitializeComponent();
for (int i = 0; i < CarLoad.Autos.Length; i++)
{
TextBlock newTxt = new TextBlock();
newTxt.Text = CarLoad.Autos[i].AutoInfos();
newTxt.Name = "TxtBlockI" + i.ToString();
newTxt.Width = 545;
newTxt.Height = 20;
newTxt.VerticalAlignment = VerticalAlignment.Top;
newTxt.HorizontalAlignment = HorizontalAlignment.Left;
newTxt.Foreground = new SolidColorBrush(Colors.White);
newTxt.FontFamily = new FontFamily("Arial Black");
newTxt.Margin = new Thickness(65, hohe, 0, 0);
TextBlock newTxt2 = new TextBlock();
newTxt2.Text = CarLoad.Autos[i].TankInfos();
newTxt2.Name = "TxtBlockT" + i.ToString();
newTxt2.Width = 545;
newTxt2.Height = 20;
newTxt2.TextWrapping = TextWrapping.Wrap;
newTxt2.VerticalAlignment = VerticalAlignment.Top;
newTxt2.HorizontalAlignment = HorizontalAlignment.Left;
newTxt2.Foreground = new SolidColorBrush(Colors.White);
newTxt2.FontFamily = new FontFamily("Arial Black");
newTxt2.Margin = new Thickness(65, hohe + 20, 0, 0);
TextBlock newTxt3 = new TextBlock();
newTxt3.Text = CarLoad.Autos[i].SonstInfos();
newTxt3.Name = "TxtBlockS" + i.ToString();
newTxt3.Width = 545;
newTxt3.Height = 20;
newTxt3.TextWrapping = TextWrapping.Wrap;
newTxt3.VerticalAlignment = VerticalAlignment.Top;
newTxt3.HorizontalAlignment = HorizontalAlignment.Left;
newTxt3.Foreground = new SolidColorBrush(Colors.White);
newTxt3.FontFamily = new FontFamily("Arial Black");
newTxt3.Margin = new Thickness(65, hohe + 40, 0, 0);
PackIcon icon = new PackIcon();
icon.Kind = PackIconKind.CarSide;
icon.Width = 50;
icon.Height = 50;
icon.Foreground = new SolidColorBrush(Colors.White);
icon.Margin = new Thickness(10, hohe, 0, 0);
hohe = hohe + 60;
gridn.Children.Add(newTxt);
gridn.Children.Add(newTxt2);
gridn.Children.Add(newTxt3);
gridn.Children.Add(icon);
}
}
}
谢谢您的帮助:)
我不会直接解决您的问题。相反,我认为最好是解决您的技术问题,因为解决此问题的方式可能仍会解决问题。
在WPF中,应用程序分为“标记”(也称为“ UI”或“前端”)和“后置代码”(也称为“代码”或“后端”)。标记是用XAML编写的,其背后的代码是C#或VB.NET。您可以使用data binding将两者链接在一起。通常的做法是在markup中创建大部分(如果不是全部)的UI。您应该avoid通过后台代码创建UI。
对于您的特定应用程序,我会考虑使用ItemsControl
,这是一个特殊控件,“可以用来显示项目集合”。它使您可以在ItemsControl
内一次声明UI标记,然后重用该模板以显示每个项目。
这里是一个基于您的问题的示例,它可能并不完美,但是应该接近您的需求:
DataTemplate
也请注意,我没有使用DataTemplate
来排列元素,这是不好的做法。 <ItemsControl ItemsSource="{Binding CarLoad.Autos}" FontFamily="Arial Black" Foreground="White">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<local:PackIcon Grid.RowSpan="3" Kind="CarSide" Width="50" Height="50" Foreground="White" Margin="10"/>
<TextBlock Grid.Column="1" Text="{Binding AutoInfos}"/>
<TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding TankInfos}"/>
<TextBlock Grid.Column="1" Grid.Row="2" Text="{Binding SonstInfos}"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
用于在元素之间提供空间,而不是给它们精确的位置。而是,WPF具有多个控件,可让您创建动态布局。在上面的示例中,我使用了Margin
控件。
如果您不熟悉任何这些术语或技术,建议您仔细阅读它们,因为它们是WPF的中坚力量。
我看过您的示例已有一段时间,我将直接回答这个问题,这与您发布的C#代码无关。相反,我怀疑您的Margin
方法在CarLoad.Autos数组中除第一个元素外的所有文本“ Marke:”之后返回包含CR和/或LF的字符串。您可以在调试器中将Grid
与Grid
进行比较以确定是否正在发生这种情况吗?