WPF,将变量传递给数据模板内的转换器

发布于 2024-07-30 04:24:14 字数 708 浏览 3 评论 0 原文

我认为这是可能的,但不确定如何做到。 我需要从数据模板内部将类级变量的值传递给转换器。

<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 是一个类变量,我无法将其正确传递给转换器。 有任何想法吗?

I assume this is possible but not sure how to do it. I need to pass the value of a class level variable to a converter, from within side a data template.

<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>

The 'Key' value exists on the response item for the data template so this gets passed correctly, whereas the CurrentLanguage is a class variable and I can't get that to pass properly to the converter. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感谢您的回复,这就是我最终需要使用的:

 <Binding Path="DataContext.CurrentLanguage" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}"/> 

Thanks for the replies, this is what I needed to use in the end:

 <Binding Path="DataContext.CurrentLanguage" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}"/> 
櫻之舞 2024-08-06 04:24:15

您可以按如下方式使用绑定对象:

<Binding Source="{x:Static local:DataObject.MyData}" />

请参阅:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c94682e5-ad16-42f9-973f-fd7588a9c0b5

You can use the binding object as follows:

<Binding Source="{x:Static local:DataObject.MyData}" />

See: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/c94682e5-ad16-42f9-973f-fd7588a9c0b5.

带上头具痛哭 2024-08-06 04:24:15

如果您将转换器定义为您拥有的资源,则可以在后面的代码中访问它。 拥有转换器后,您就可以为其设置属性。

var myVisConverter = (VisibilityConverter)window.Resources["VisibilityConverter"];
myVisConverter.CurrentLanguage = ...

编辑 好的,如果您尝试从 DataTemplate 内访问父 DataContext,有几个选项。 最简单的方法是使用正确的 DataContext 命名控件,然后像这样绑定到该控件...

<Binding Path="DataContext.CurrentLanguage" ElementName="nameGivenToElement" />

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.

var myVisConverter = (VisibilityConverter)window.Resources["VisibilityConverter"];
myVisConverter.CurrentLanguage = ...

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...

<Binding Path="DataContext.CurrentLanguage" ElementName="nameGivenToElement" />

Josh Smith wrote an article with more ways of getting inherited DataContexts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文