wpf 绑定到父数据上下文(超出项目源)

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

Bind to parent datacontext (out of itemsource)

wpfwindows-phone-8mvvmbindingcaliburn.micro

提问by devha

In my Windows Phone 8 app I have list of items. I have defined ItemTemplate for list items.

在我的 Windows Phone 8 应用程序中,我有项目列表。我已经为列表项定义了 ItemTemplate。

I want to display in each of these items one value from view model, not from the list item itself. How should I set up the binding from list item to viewmodel.

我想在这些项目中的每一个中显示一个来自视图模型的值,而不是来自列表项本身。我应该如何设置从列表项到视图模型的绑定。

My data template is like this:

我的数据模板是这样的:

<DataTemplate x:Key="template">
    <StackPanel>
        <TextBlock Text="{Binding Name}"/> <!-- From list item -->
        <TextBlock Text="{Binding MyViewModel.Country ?? }"/> <!-- From view model -->
    </StackPanel>  
</DataTemplate>

So how to bind Country property to view model, not list item (in item source).

那么如何将 Country 属性绑定到视图模型,而不是列表项(在项源中)。

回答by Rohit Vats

Since you tagged your question with WPF, I can tell you the way of doing it in WPF, you can validate if that can be reused in Windows phone 8 apps.

由于您用 标记了您的问题WPF,我可以告诉您在 WPF 中执行此操作的方式,您可以验证它是否可以在 Windows phone 8 应用程序中重复使用。

First, you can give x:Nameto root element to which ViewModel is bind to. Say it's window, set x:Name on it and bind using ElementName.

首先,您可以指定x:NameViewModel 绑定到的根元素。假设它是窗口,在其上设置 x:Name 并使用ElementName.

<Window x:Name="myWindow">
  ...
  <DataTemplate x:Key="template">
    <StackPanel>
        <TextBlock Text="{Binding Name}"/> <!-- From list item -->
        <TextBlock Text="{Binding DataContext.MyViewModel.Country,
                            ElementName=myWindow }"/> <!-- From view model -->
    </StackPanel>  
</DataTemplate>
</Window>


Second, you can try using RelativeSourceto travel Visual tree and get root element DataContext.

其次,您可以尝试使用RelativeSource遍历可视化树并获取根元素DataContext。

<DataTemplate x:Key="template">
    <StackPanel>
        <TextBlock Text="{Binding Name}"/> <!-- From list item -->
        <TextBlock Text="{Binding DataContext.MyViewModel.Country,
                           RelativeSource={RelativeSource Mode=FindAncestor, 
                                                   AncestorType=Window} }"/>
                         <!-- From view model -->
    </StackPanel>  
</DataTemplate>


Moreove, if ListBox is inheriting DataContext from root element (i.e. you haven't explicitly set DataContext on ListBox). You can use both approaches on ListBox as well in place of Window.

此外,如果 ListBox 从根元素继承 DataContext(即您没有在 ListBox 上显式设置 DataContext)。您也可以在 ListBox 上使用这两种方法来代替 Window。

Note- As mentioned here, FindAncestoris not defined for Windows phone 8 but element name does work. So, try using first approach and it should work for you.

-如前所述这里FindAncestor为Windows 8手机没有定义,但元素的名称确实工作。所以,尝试使用第一种方法,它应该适合你。