绑定的StringFormat [英] Binding StringFormat

查看:121
本文介绍了绑定的StringFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我要展示,我需要进行不同的显示的每个文字块的文本的TextBlocks的集合。我目前在节能标签属性中的格式字符串,我需要来显示这种格式的文本。我如何绑定的StringFormat节?

I have a collection of textblocks that I'm going to be showing and I'm needing the text of each textblock to be displayed differently. I'm currently saving the format string in the tag property and I'm needing to display the text in this format. How do I bind the StringFormat section?

类似下面的部分:

< TextBlock的标签={结合MyFormatString}文本={结合myProperty的,的StringFormat ='{} {0:MyTag的}'}/>

推荐答案

由于 BindingBase.StringFormat 不是一个依赖项属性,我不认为你可以绑定。如果格式化字符串变化,恐怕你将不得不求助于像这样

Since BindingBase.StringFormat is not a dependency property, I do not think that you can bind it. If the formatting string varies, I'm afraid you will have to resort to something like this

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

和做的格式在您的视图模型。或者,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx\">MultiBinding和转换器(例如code未经测试):

and do the formatting in your view model. Alternatively, you could use a MultiBinding and a converter (example code untested):

<TextBlock>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource myStringFormatter}">
            <Binding Path="MyProperty" />
            <Binding Path="MyFormatString" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

public class StringFormatter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Format((string)values[1], values[0]);
    }
    ...
}

这篇关于绑定的StringFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆