简体   繁体   English

绑定到父数据上下文

[英]Binding to parent datacontext

My code behind as follows 我的代码如下

this.DataContext = MainWindowViewModel.Instance;

xaml that I tried 我尝试过的XAML

<ComboBox ItemsSource="{Binding Vendors, 
    RelativeSource={RelativeSource AncestorType=Window}}" 
    Visibility="{Binding RepairHasNoVendors, 
    Converter={StaticResource VisibilityOfBool}}"/>

My error 我的错误

Error: 40 : BindingExpression path error: 'Vendors' property not found on 'object' 
''MainWindow' (Name='')'. BindingExpression:Path=Vendors; DataItem='MainWindow' 
(Name='')

As I understand, I've set my window datacontext in codebehind and I thought that by specifying relaticesource to be window I'd have access to those properties? 据我了解,我已经在代码隐藏中设置了窗口数据上下文,并且我认为通过将relaticesource指定为窗口,我可以访问那些属性吗?

My viewmodel has that property 我的视图模型具有该属性

public ObservableCollection<VendorViewModel> Vendors

I also wanted to add that my combobox is inside datagridtemplatecolumn whos datacontext is set properly and I'm able to access those properties, for example my datagrid has datagridtextcolumn and binding works on it 我还想补充一下,我的组合框位于datagridtemplatecolumn中,其datacontext设置正确,并且我能够访问那些属性,例如,我的datagrid具有datagridtextcolumn并对其进行绑定工作

<DataGridTextColumn Header="Repair Id" Width="Auto" Binding="{Binding RepairID}"/>

You need to bind to a property of window's datacontext not the window 您需要绑定到窗口数据datacontext的属性,而不是window

<ComboBox ItemsSource="{Binding Path=DataContext.Vendors, 
    RelativeSource={RelativeSource AncestorType=Window}}" 
    Visibility="{Binding RepairHasNoVendors, 
    Converter={StaticResource VisibilityOfBool}}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM