簡體   English   中英

用連字符包裝單詞不起作用

[英]Wrapping word with Hyphen not work

我有一個文本塊,其中包含帶有連字符的消息。 我希望所有包含連字符的單詞都像一個真正的單詞一樣被解釋為不間斷的單詞。 我暫時得到了這個:

test test test test Test-
test

而且我要

test test test test 
Test-test

我覺得連字符是一個單詞中用於分隔的特殊字符,這就是為什么我不能將單詞包裝起來的原因。 我嘗試使用此IsHyphenationEnabled,但我真的不知道他的工作...

!!!注意!!! 我不想破壞單詞,當帶有連字符的單詞不能插入一行時,因為該單詞沒有空格,我想換一行

謝謝你們,最好的問候。

這是一個將值轉換為僅用於顯示的示例。

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

上面的例子是為了說明目的,也可以直接綁定到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();
        }
    }
}

結果

結果

您會看到轉換器可以修改顯示的值而無需修改源。

我通過確定應在何處手動放置換行符解決了這個問題。 就我而言,我在控件上有一個方法,該方法包含將文本輸入的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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM