如何在wpf文本块中显示一行文本

发布于 2024-08-18 23:11:48 字数 251 浏览 10 评论 0原文

我是 wpf 的新手,我想在 wpf 文本块中的一行中显示文本。 例如:

<TextBlock 
    Text ="asfasfasfa
    asdasdasd"
</TextBlock>

TextBlock 默认以两行显示它,

但我希望它仅在一行中显示,如“asafsf asfafaf”。我的意思是在一行中显示所有文本,即使文本中有不止一行
我应该怎么办?

I'm a newbie with wpf , what i want to display the text in one line in wpf textblock.
eg.:

<TextBlock 
    Text ="asfasfasfa
    asdasdasd"
</TextBlock>

TextBlock display it in two lines default,

but i want it in only one line like this"asafsf asfafaf". I mean show all the text in one line even there are more than one lines in the text
what should i do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

吾家有女初长成 2024-08-25 23:11:48

使用转换器:

    <TextBlock Text={Binding Path=TextPropertyName,
Converter={StaticResource SingleLineTextConverter}}

SingleLineTextConverter.cs:

public class SingleLineTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string s = (string)value;
        s = s.Replace(Environment.NewLine, " ");
        return s;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Use a Converter:

    <TextBlock Text={Binding Path=TextPropertyName,
Converter={StaticResource SingleLineTextConverter}}

SingleLineTextConverter.cs:

public class SingleLineTextConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string s = (string)value;
        s = s.Replace(Environment.NewLine, " ");
        return s;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
你的往事 2024-08-25 23:11:48

而不是这样:

            <TextBlock Text="Hello
                How Are
                You??"/>

使用 this:

            <TextBlock>
                Hello
                How Are
                You??
            </TextBlock>

或 this:

            <TextBlock>
                <Run>Hello</Run> 
                <Run>How Are</Run> 
                <Run>You??</Run>
            </TextBlock>

或在代码隐藏中设置 Text 属性,如下所示:

(在 XAML 中)

            <TextBlock x:Name="MyTextBlock"/>

(在代码 - c# 中)

            MyTextBlock.Text = "Hello How Are You??"

代码隐藏方法有一个优点是您可以在设置之前格式化文本。
示例:如果从文件中检索文本并且您想要删除任何回车换行符,您可以这样做:

 string textFromFile = System.IO.File.ReadAllText(@"Path\To\Text\File.txt");
 MyTextBlock.Text = textFromFile.Replace("\n","").Replace("\r","");

Instead of this:

            <TextBlock Text="Hello
                How Are
                You??"/>

Use this:

            <TextBlock>
                Hello
                How Are
                You??
            </TextBlock>

or this:

            <TextBlock>
                <Run>Hello</Run> 
                <Run>How Are</Run> 
                <Run>You??</Run>
            </TextBlock>

or set Text property in code behind like this :

(In XAML)

            <TextBlock x:Name="MyTextBlock"/>

(In code - c#)

            MyTextBlock.Text = "Hello How Are You??"

Code-behind approach has an advantage that you can format your text before setting it.
Example: If the text is retrieved from a file and you want to remove any carriage-return new-line characters you can do it this way:

 string textFromFile = System.IO.File.ReadAllText(@"Path\To\Text\File.txt");
 MyTextBlock.Text = textFromFile.Replace("\n","").Replace("\r","");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文