WPF,将变量传递给数据模板内的转换器
我认为这是可能的,但不确定如何做到。 我需要从数据模板内部将类级变量的值传递给转换器。
<DataTemplate x:Key="ResponseItemTemplate">
<StackPanel Orientation="Horizontal" >
<StackPanel.Visibility>
<MultiBinding Converter="{StaticResource VisibilityConverter}">
<Binding Path="Key"/>
<Binding Path="CurrentLanguage"/>
</MultiBinding>
</StackPanel.Visibility>
<TextBox Width="200" Text="{Binding Value}" />
</StackPanel>
</DataTemplate>
“Key”值存在于数据模板的响应项上,因此可以正确传递,而 CurrentLanguage 是一个类变量,我无法将其正确传递给转换器。 有任何想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢您的回复,这就是我最终需要使用的:
Thanks for the replies, this is what I needed to use in the end:
您可以按如下方式使用绑定对象:
请参阅:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c94682e5-ad16-42f9-973f-fd7588a9c0b5。
You can use the binding object as follows:
See: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c94682e5-ad16-42f9-973f-fd7588a9c0b5.
如果您将转换器定义为您拥有的资源,则可以在后面的代码中访问它。 拥有转换器后,您就可以为其设置属性。
编辑 好的,如果您尝试从 DataTemplate 内访问父 DataContext,有几个选项。 最简单的方法是使用正确的 DataContext 命名控件,然后像这样绑定到该控件...
Josh Smith 编写了一个 文章提供了更多获取继承 DataContext 的方法。
If you define the converter as a resource, which you have, you can access it in the code behind. Once you have the converter you can then set a property on it.
EDIT Ok, if you're trying to get access to the parent DataContext from within the DataTemplate, there's a couple of options. Easiest is to name the control with the correct DataContext, then bind to that control like so...
Josh Smith wrote an article with more ways of getting inherited DataContexts.