user3928549
user3928549

Reputation: 5

Binding to parent datacontext

My code behind as follows

this.DataContext = MainWindowViewModel.Instance;

xaml that I tried

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

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

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

Upvotes: 0

Views: 3956

Answers (1)

Bolu
Bolu

Reputation: 8786

You need to bind to a property of window's datacontext not the window

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

Upvotes: 2

Related Questions