如何将多个值绑定到单个 WPF TextBlock?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2552853/
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
How to bind multiple values to a single WPF TextBlock?
提问by Spen D
I'm currently using the TextBlock
below to bind the value of a property named Name
:
我目前正在使用TextBlock
以下内容绑定名为 的属性的值Name
:
<TextBlock Text="{Binding Name}" />
Now, I want to bind anotherproperty named ID
to the same TextBlock
.
现在,我想将另一个名为ID
的属性绑定到相同的TextBlock
.
Is it possible to bind two or more values to the same TextBlock
? Can it be done with simple concatenation, like Name + ID
and, if not, how else could this be approached?
是否可以将两个或多个值绑定到相同的值TextBlock
?是否可以通过简单的连接来完成,例如Name + ID
,如果不是,还有其他方法可以解决吗?
回答by Richard McGuire
You can use a MultiBinding
combined with the StringFormat
property. Usage would resemble the following:
您可以MultiBinding
与StringFormat
属性结合使用。用法类似于以下内容:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} + {1}">
<Binding Path="Name" />
<Binding Path="ID" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Giving Name
a value of Foo
and ID
a value of 1
, your output in the TextBlock would then be Foo + 1
.
给定Name
一个Foo
和ID
一个 的值1
,您在 TextBlock 中的输出将是Foo + 1
。
Note:
that this is only supported in .NET 3.5 SP1 and 3.0 SP2 or later.
Note:
这仅在 .NET 3.5 SP1 和 3.0 SP2 或更高版本中受支持。
回答by Patrick
I know this is a way late, but I thought I'd add yet another way of doing this.
我知道这有点晚了,但我想我会添加另一种方式来做到这一点。
You can take advantage of the fact that the Text property can be set using "Runs", so you can set up multiple bindings using a Run for each one. This is useful if you don't have access to MultiBinding (which I didn't find when developing for Windows Phone)
您可以利用可以使用“ Runs”设置 Text 属性这一事实,因此您可以使用 Run 为每个绑定设置多个绑定。如果您无权访问 MultiBinding(我在为 Windows Phone 开发时没有找到),这很有用
<TextBlock>
<Run Text="Name = "/>
<Run Text="{Binding Name}"/>
<Run Text=", Id ="/>
<Run Text="{Binding Id}"/>
</TextBlock>
回答by CodeWarrior
If these are just going to be textblocks (and thus one way binding), and you just want to concatenate values, just bind two textblocks and put them in a horizontal stackpanel.
如果这些只是文本块(因此是一种方式绑定),并且您只想连接值,只需绑定两个文本块并将它们放在水平堆栈面板中。
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding ID}"/>
</StackPanel>
That will display the text (which is all Textblocks do) without having to do any more coding. You might put a small margin on them to make them look right though.
这将显示文本(这是所有 Textblocks 所做的),而无需再进行任何编码。不过,您可能会在它们上留一点边距,以使它们看起来不错。
回答by Preet Sangha
Use a ValueConverter
使用值转换器
[ValueConversion(typeof(string), typeof(String))]
public class MyConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Format("{0}:{1}", (string) value, (string) parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
and in the markup
并在标记中
<src:MyConverter x:Key="MyConverter"/>
. . .
. . .
<TextBlock Text="{Binding Name, Converter={StaticResource MyConverter Parameter=ID}}" />