2

我认为这是可能的,但不知道该怎么做。我需要从数据模板内部将类级别变量的值传递给转换器。

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

4

3 回答 3

1

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

 <Binding Path="DataContext.CurrentLanguage" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Window}}"/> 
于 2009-07-27T15:22:22.187 回答
0

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

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

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

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

Josh Smith 写了一篇文章,介绍了获取继承 DataContexts 的更多方法。

于 2009-07-27T13:46:19.730 回答
0

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

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

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

于 2009-07-27T14:39:45.723 回答