【问题标题】:C# Text wraps after first wordC# 文本在第一个单词后换行
【发布时间】:2020-05-24 20:02:35
【问题描述】:

我有问题。我的 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);
            }
        }
    }

感谢您的帮助:)

【问题讨论】:

    标签: c# wpf wpf-controls


    【解决方案1】:

    我不会直接解决您的问题。相反,我认为最好解决您的技术问题,因为修复您正在处理的方式可能会解决问题。

    在 WPF 中,应用程序分为“标记”(也称为“UI”或“前端”)和“代码隐藏”(也称为“代码”或“后端”)。标记是用 XAML 编写的,代码隐藏是 C# 或 VB.NET。您可以使用data binding 将两者链接在一起。通常的做法是在标记中创建大部分(如果不是全部)UI。您应该避免通过代码隐藏创建 UI。

    对于您的特定应用程序,我会考虑使用ItemsControl,这是一个“可用于呈现项目集合”的特殊控件。它允许您在 DataTemplate 内声明一次 UI 标记,然后重用该模板来显示每个项目。

    这是一个基于您的问题的示例,它可能并不完美,但应该接近您的需求:

    <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>
    

    还要注意,我没有使用Margin 排列元素,这是不好的做法。 Margin 用于在元素之间提供空间,但不是为它们提供精确位置。相反,WPF 有几个控件允许您创建动态布局。在上面的示例中,我使用了Grid 控件。

    如果您不熟悉这些术语或技术,我建议您阅读它们,因为它们是 WPF 的支柱。

    【讨论】:

    • 谢谢,这确实整理了我的代码很多 :D 但是问题仍然存在。只有第一个有效。我的 AutoInfos 代码:public string AutoInfos() { return "Marke: " + marke + ", Modell: " + modell + ", Farbe: " + farbe + ", PS: " + ps; }
    【解决方案2】:

    我看了你的例子一段时间,我打算直接回答,这与你发布的 C# 代码无关。相反,我怀疑您的 CarLoad.Autos[i].AutoInfos(); 方法在 CarLoad.Autos 数组中除第一个元素之外的所有元素的文本“Marke:”之后返回一个包含 CR 和/或 LF 的字符串。您能否在调试器中将CarLoad.Autos[0].AutoInfos(); 与CarLoad.Autos[1].AutoInfos(); 进行比较以确定是否发生了这种情况?

    【讨论】:

    • 你可能是对的。如果TextWrapping 没有在任何地方设置,那么在字符串的某个地方一定有一些换行符。
    猜你喜欢
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    相关资源
    最近更新 更多