wpf 绑定时如何在xaml中进行计算?

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

How to do calculation in xaml while binding?

wpfxamlbinding

提问by user763554

I can bind a label to a slider as shown below:

我可以将标签绑定到滑块,如下所示:

<Label Name="lblTest" Content="{Binding ElementName=slider1, Path=Value}"/>

How do I bind another label to the same slider with display value of, say, 30% of the value of slider? How do I do the calculation in xaml and not in my code? Thanks.

如何将另一个标签绑定到同一个滑块,显示值为滑块值的 30%?如何在 xaml 中而不是在我的代码中进行计算?谢谢。

采纳答案by H.B.

You cannot do the calculation in XAML, you could however apply some converterwhich does it for you.

你不能在 XAML 中进行计算,但是你可以应用一些为你做的转换器

This question might be relevant: How do you change a bound value, reverse it, multiply it, subtract from it or add to it?

这个问题可能是相关的:您如何更改绑定值、反转它、乘以它、从中减去或添加到它?

回答by xmedeko

There are already a few projects which bring expressions to Bindigs, e.g. see CalcBindingor QuickConverter.

已经有一些项目将表达式引入 Bindigs,例如参见 CalcBindingQuickConverter