将一个元素绑定到两个源 [英] Bind an element to two sources
问题描述
我目前有两个文本框可以接受任何数字.我有一个文本块,它接受输入的两个数字并计算平均值.
I currently have two text boxes which accept any number. I have a text block that takes the two numbers entered and calculates the average.
我想知道是否有一种方法可以将此文本块绑定到两个文本框并使用自定义转换器来计算平均值?我目前正在捕获两个文本框上的文本更改事件并以这种方式计算平均值,但我假设数据绑定会更有效和更容易.
I was wondering if there was a way I could bind this text block to both text boxes and utilize a custom converter to calculate the average? I currently am catching the text changed events on both text boxes and calculating the average that way, but I am under the assumption data binding would be more efficient and easier.
推荐答案
您正在寻找 MultiBinding
.
You're looking for MultiBinding
.
您的 XAML
将如下所示:
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource myConverter}">
<Binding Path="myFirst.Value" />
<Binding Path="mySecond.Value" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
合理替换 myConverter
、myFirst.Value
和 mySecond.Value
.
这篇关于将一个元素绑定到两个源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!