.net 将文本添加到绑定的 TextBlock

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7130859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:50:10  来源:igfitidea点击:

Adding text to a bound TextBlock

.netwpfxamldata-bindingtextblock

提问by Rhys

I would like to prepend a text in a data-bound text block:

我想在数据绑定文本块中添加一个文本:

<TextBlock Text="{Binding Title}" />

The text that is shown is:

显示的文本是:

"My title"

What I want to be shown is:

我想展示的是:

This is "My title"

回答by Shebin

You can use the StringFormatproperty of the binding:

您可以使用StringFormat绑定的属性:

 <TextBlock Text="{Binding Title, StringFormat=This is {0}}"></TextBlock> 

Check out this blog post for more information: WPF String.Format in XAML with the StringFormat attribute.

查看此博客文章了解更多信息:WPF String.Format in XAML with the StringFormat attribute

回答by H.B.

If you want to do it in the binding:

如果您想在绑定中执行此操作:

<TextBlock Foreground="#FFC8AB14" FontSize="28">
    <TextBlock.Text>
        <Binding Path="Title">
            <Binding.StringFormat>
                This is "{0}"
            </Binding.StringFormat>
        </Binding>
    </TextBlock.Text>
</TextBlock>

Element syntax required to escape quotes. If the quotes where just to mark the inserted text and should not appear in the output it is much easier of course:

转义引号所需的元素语法。如果引号只是为了标记插入的文本而不应该出现在输出中,那当然要容易得多:

<TextBlock Text="{Binding Title, StringFormat={}This is {0}}" Foreground="#FFC8AB14" FontSize="28">

回答by grantnz

You could do this with a converter.

你可以用转换器来做到这一点。

<TextBlock Text="{Binding Title, ConverterParameter=This is, Converter={StaticResource TextPrefixConverter}}" Foreground="#FFC8AB14" FontSize="28" />

The converter would simply prefix the bound value with the ConverterParameter.

转换器将简单地使用 ConverterParameter 作为绑定值的前缀。

public class TextPrefixConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {                        
        String result = String.Empty;
        if ( parameter != null)
            result = parameter.ToString( );

        if (value != null)
            result += value.ToString( );

        return result;
    }
...
}

It's not obvious is the spaces and/or quotes are intended to be part of the output. If so, the converter could be changed to trim the spaces and/or add quotes to the constructed string.

不明显的是空格和/或引号旨在成为输出的一部分。如果是这样,可以更改转换器以修剪空格和/或向构造的字符串添加引号。

Another way of doing this is:

另一种方法是:

<TextBlock Foreground="#FFC8AB14" FontSize="28" >
    <Run Text="This is " />
    <Run Text="{Binding Path=Title}" />       
</TextBlock>

回答by grantnz

Hi You can write as following:

您好,您可以这样写:

<TextBlock>
     <TextBlock>This is </TextBlock>
     <TextBlock Text="{Binding Title}"></TextBlock>
</TextBlock>

回答by Kishore Kumar

just use StringFormat for formatting purpose.

只需使用 StringFormat 进行格式化即可。

<TextBlock Text="{Binding Title,StringFormat='This is {0}'}" Foreground="#FFC8AB14" FontSize="28" />

回答by Alexander Abakumov

The best approach here in terms of performance, as already answered, is using StringFormatfor Bindingand assign it to the Textproperty of TextBlock.

正如已经回答的那样,这里在性能方面的最佳方法是使用StringFormatforBinding并将其分配给 的Text属性TextBlock

However if performance isn't a concern, and XAML readability is preferred, another approach is to use Runinside TextBlock:

但是,如果性能不是问题,并且首选 XAML 可读性,另一种方法是使用Runinside TextBlock

<TextBlock Foreground="#FFC8AB14" FontSize="28">
    This is <Run Text="{Binding Title}" />
</TextBlock>

Also, this way you can apply different styles (text/background color, italic/bold font, font size, etc.) to different parts of your TextBlock, which is something you can't do with Binding's StringFormat. And this is way more efficient than having multiple TextBlocks with different text/background styles.

此外,这种方式可以应用不同的样式(文本/背景颜色,斜体/粗体字体,字体大小等),以您的不同部分TextBlock,这是你不能做BindingStringFormat。这比使用多个TextBlock具有不同文本/背景样式的 s更有效。