wpf 如何传递 IValueConverter 参数?

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

How to Pass IValueConverter Parameter?

wpfxaml

提问by MVK

In XAML, <Grid x:Name="MainGrid3">, Here I want to pass MainGrid3as a parameter of IValueConverter. How can I do this?enter image description here

在 XAML 中<Grid x:Name="MainGrid3">,这里我想MainGrid3作为IValueConverter. 我怎样才能做到这一点?在此处输入图片说明

回答by Matas

You have ConverterParameterinside your binding, where you can use another binding with ElementNameof your grid.

您有ConverterParameter内部绑定,您可以在其中使用ElementName网格的另一个绑定。

<Grid Name="MainGrid3"></Grid>
<TextBlock Text="{Binding SomeBinding, Converter={StaticResource SomeConverter}, ConverterParameter={Binding ElementName=MainGrid3}}"></TextBlock>

Edit:Ok, so apparently I was wrong, you can't use bindings inside ConverterParameter as it is not a dependency property. Working solution would be to use x:Referencelike so:

编辑:好的,显然我错了,您不能在 ConverterParameter 中使用绑定,因为它不是依赖属性。工作解决方案是这样使用x:Reference

<Grid Name="MainGrid3"></Grid>
<TextBlock Text="{Binding SomeBinding, Converter={StaticResource SomeConverter}, ConverterParameter={x:Reference Name=MainGrid3}}"></TextBlock>