wpf 我们可以在数据绑定中连接两个属性吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4325453/
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-08 22:21:51  来源:igfitidea点击:

Can we concat two properties in data binding?

wpfsilverlight-4.0

提问by TCM

Can we concat two properties together in binding expression? If possible without converter or without writing two textblocks and setting them individually?

我们可以在绑定表达式中将两个属性连接在一起吗?如果可能没有转换器或没有编写两个文本块并单独设置它们?

采纳答案by Tom

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

就像 alpha-mouse 建议 MultiBinding 不能开箱即用一样,但是这个人已经把一些东西放在一起可能会有所帮助:

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

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

If that seems a bit rogue, then maybetry 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; }
}

回答by Nawaz

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

如果你想显示,说FirstNameLastName,在一个单一的TextBlock,那么你可以这样做:

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

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

现在,TextBlock 的Text属性将在以下"Sachin Tendulkar"情况下显示:

FirstName = Sachin
LastName  = Tendulkar

Hope that helps.

希望有帮助。

回答by Kiran k g

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

data1 and data2 are string properties which are binded.

data1 和 data2 是绑定的字符串属性。

回答by alpha-mouse

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

借助MultiBinding和 StringFormat,可以在 WPF 中实现。但不幸的是,在 Silverlight 中不是。

回答by reza.cse08

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

如果您需要添加任何字符串,请尝试。这里我在windows phone中绑定文本后添加“%”。

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

回答by Rune Grimstad

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

您可以使用执行串联的 getter 添加新属性。

Say you have FirstNameand LastNameproperties. You can then define a Nameproperty as follows:

说你有FirstNameLastName属性。然后,您可以Name按如下方式定义属性:

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.

这会工作得很好,但您应该知道不能对只读属性进行双向绑定。此外,您可能希望为源属性的 setter 中的串联属性实现属性更改通知。