Can we concat two properties in data binding?

大兔子大兔子 提交于 2019-12-03 00:57:45

Like alpha-mouse suggests MultiBinding won't work out of the box, but this guy has thrown something together that might help:

http://www.olsonsoft.com/blogs/stefanolson/post/Improvements-to-Silverlight-Multi-binding-support.aspx

If that seems a bit rogue, then maybe try putting a combined value property on your object as a helper for the Binding mechanism, like:

public string FullName {
   get { return this.FirstName + " " + this.LastName; }
}

If you want to show, say FirstName and LastName, in a single TextBlock, then you can do like this:

<TextBlock>
     <Run Text="{Binding FirstName}" />
     <Run Text="   " /> <!-- space -->
     <Run Text="{Binding LastName}" />
</TextBlock>

Now, the TextBlock's Text property will be "Sachin Tendulkar" and will be displayed if:

FirstName = Sachin
LastName  = Tendulkar

Hope that helps.

<TextBlock.Text>
   <MultiBinding StringFormat="{}{0} , {1}">
     <Binding Path="data1" />
     <Binding Path="data2" />
    </MultiBinding>
</TextBlock.Text>

data1 and data2 are string properties which are binded.

It is possible in WPF with the help of MultiBinding and StringFormat. But not in Silverlight unfortunately.

If you need to add any string, then try it. Here I add "%" after binding text in windows phone.

<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}%}"/>
Rune Grimstad

You can add a new property with a getter that performs the concatenation.

Say you have FirstName and LastName properties. You can then define a Name property as follows:

public string Name { get { return FirstName + " " + LastName; } }

This will work well, but you should be aware that you cannot do two-way binding for a read-only property. Also you may want to implement property changed notification for the concatenated property in the setters for the source properties.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!