用连字符包装词不起作用

时间:2014-08-14 06:17:38

标签: c# wpf wrapping

我有一个包含带有连字符的邮件的文本块。我希望包含连字符的所有单词都像一个真正的单词一样解释,并且具有不间断的空格。 我暂时得到了这个:

test test test test Test-
test

我想要

test test test test 
Test-test

我觉得连字符是一个特殊的字符,用于分隔单词,这就是为什么我不能包装这个单词。但是,这样做有什么想法吗? 我尝试使用这个IsHyphenationEnabled,但我真的不知道他的工作......

!!!注意!!!我不想破解这个词,当一个带连字符的单词不能插入一行时,我想要一个新的行,因为它没有任何空格用于单词

谢谢你们, 最好的问候。

2 个答案:

答案 0 :(得分:0)

这是一个转换值仅供显示的示例。

<Grid xmlns:l="clr-namespace:CSharpWPF"
      xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Grid.Resources>
        <sys:String x:Key="testString">some string with non-breaking space</sys:String>
        <l:HyphenRemover x:Key="HyphenRemover" />
    </Grid.Resources>
    <StackPanel>
        <TextBlock>
            <Run Text="Nomal string: " />
            <Run Text="{StaticResource testString}" />
        </TextBlock>
        <TextBlock>
            <Run Text="Hyphen removed: " />
            <Run Text="{Binding Source={StaticResource testString}, Converter={StaticResource HyphenRemover},Mode=OneWay}" />
        </TextBlock>
    </StackPanel>
</Grid>

以上示例是针对ilustration purpoes,绑定也可以直接进行到textblock

例如

<TextBlock Text="{Binding Source={StaticResource testString}, Converter={StaticResource HyphenRemover}}" />

HyphenRemover转换器

namespace CSharpWPF
{
    class HyphenRemover : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string text = value as string;
            if (!string.IsNullOrWhiteSpace(text))
            {
                return text.Replace("-", string.Empty);
            }
            return value;
        }

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

结果

result

您可以看到转换器能够在不修改源的情况下修改显示值。

答案 1 :(得分:0)

我通过确定手动放置换行符的位置解决了这个问题。在我的例子中,我在控件上有一个方法,它包含我提供文本的TextBlock,但如果你想在XAML中设置文本,这也可以在依赖属性中使用。

public void ProcessText(TextBlock target, string text)
{
    //get display info about text block
    var maxLength = target.ActualWidth - target.Padding.Left - target.Padding.Right;
    var textBlockTypeFace = new Typeface(target.FontFamily, target.FontStyle, target.FontWeight,
        target.FontStretch);

    var words = text.Split(' ');
    var currentLine = "";

    for (var i = 0; i < words.Length; i++)
    {
        var word = words[i];

        //account for spaces between words if it's not the last word
        if (i == words.Length - 1) {
            word += " ";
        }

        var formattedText = new FormattedText(currentLine + word, CultureInfo.InvariantCulture,
            FlowDirection.LeftToRight, textBlockTypeFace, target.FontSize, target.Foreground);

        //if the line we're building fits, add the word to the text block and keep building the current line
        //otherwise, add a line break to the text block before adding the word and reset the current line
        if (formattedText.Width < maxLength)
        {
            currentLine += word;
            target.Text += word;
        }
        else
        {
            target.Text += Environment.NewLine + word;
            currentLine = word;
        }
    }
}