user1620696
user1620696

Reputation: 11375

WPF Command Binding to ancestor property

I have a doubt about binding to an ancestor property in WPF. My situation is: my window has a view model as data context with all the commands as ICommand properties. I have a list of checkboxes as follows:

<ItemsControl ItemsSource="{Binding CurrentCustomer.SuppiersSelection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <CheckBox FontSize="16" Content="{Binding Path=Supplier.Company}" 
                      IsChecked="{Binding Path=Selected}"></CheckBox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The question is: I want that each checkbox has a command bound to it, so that when a supplier is checked or unchecked the relationship between customer and supplier is automatically saved. My problem is that the command properties are on the Window Data Context and this checkboxes is using as data context the ItemsSource of ItemControl. How do I bind to the property on the Window Data Context?

Thanks in advance for your help.

Upvotes: 1

Views: 4718

Answers (1)

Pavel Voronin
Pavel Voronin

Reputation: 13983

{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=DataContext}

But it's not clear what is the task. Why don't you use two way binding and process the change of property Selected?

Upvotes: 3

Related Questions