绑定到转换器参数
是否可以在 Silverlight 4.0 中绑定到 ConverterParameter?
例如,我想做这样的事情,并将 ConverterParameter 绑定到 ViewModel 中的一个对象。
如果这不可能,还有其他选择吗?
<RadioButton
Content="{Binding Path=Mode}"
IsChecked="{Binding
Converter={StaticResource ParameterModeToBoolConverter},
ConverterParameter={Binding Path=DataContext.SelectedMode,ElementName=root}}"
/>
Is it possible to bind to a ConverterParameter in Silverlight 4.0?
For instance I would like to do something like this and bind the ConverterParameter to an object in a ViewModel for instance.
If this is not possible are there any other options?
<RadioButton
Content="{Binding Path=Mode}"
IsChecked="{Binding
Converter={StaticResource ParameterModeToBoolConverter},
ConverterParameter={Binding Path=DataContext.SelectedMode,ElementName=root}}"
/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是,不能,您无法绑定到 ConverterParameter。我过去使用过两个选项:不使用转换器,而是在 ViewModel(或您绑定的任何内容)上创建一个属性来为您进行转换。如果您仍然想走转换器路线,请将整个绑定对象传递给转换器,然后您可以这样进行计算。
Unfortunetly no, you can't bind to a ConverterParameter. There's two options I've used in the past: instead of using a Converter, create a property on your ViewModel (or whatever you're binding to) which does the conversion for you. If you still want to go the Converter route, pass the entire bound object to the converter and then you can do your calculation that way.
另一种选择是通过创建一个自定义转换器来包装其他转换器并从属性传入转换器参数。只要这个自定义转换器继承了DependencyObject并使用了DependencyProperty,就可以绑定到。例如:
Another option is to get fancy by creating a custom converter that wraps your other converter and passes in a converter param from a property. As long as this custom converter inherits DependencyObject and uses a DependencyProperty, it can be bound to. For example:
我知道这是一个老问题,但也许这对遇到它的人有用。我找到的解决方案如下:
在您的xaml中:
I know it's an old question but maybe this will be useful to somebody who came across it. The solution I found is as follow:
and in your xaml:
可以通过创建支持绑定到 ConverterParameter 的自己的 Binding 来实现。以下是如何使用它:
以及实现此绑定的代码:
It is possible by creating an own Binding which supports binding to the ConverterParameter. Here is how to use it:
And the code with the implementation for this binding:
我找到了一篇相关的SO帖子,我相信它回答了这个问题:
WPF ValidationRule with dependency property
在我的具体示例中,我最终得到的 xaml 看起来像这样实现了上面的示例:
I have found a related SO post that I believe answers this question:
WPF ValidationRule with dependency property
In my specific example I end up with xaml that looks like this having implemented the above example: