Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf: FindAncestor of Ancestor

Tags:

.net

binding

wpf

I have a UserControl that is bound to a ViewModel. On the UserControl is bound to a List of items (class objects). So when I want to bind various element to properties on the RowDetails template of the DataGrid, I use:

"{Binding RelativeSource={RelativeSource FindAncestor, 
                          AncestorType={x:Type UserControl}}, 
               Path=DataContext.SomeProperty}"

This works fine. Now I want to factor out the RowDetails stuff to a UserControl I can use in other top level UserControls with DataGrids on them. So how do i find a UserControl that is the ancestor of the UserControl in which this binding statement is written?

I can't specify the name of top level UserControl in the x:Type declaration because it is different every time.

like image 348
GilShalit Avatar asked Jun 19 '11 14:06

GilShalit


1 Answers

You could do this with AncestorLevel, however I seriously doubt this is the right way to bind.

You will have a UserControl that works depending on its position in the logical tree.

If you would insert some other usercontrol in the middle of those two it would stop working.

The right way to bind is to use the Datacontext that is provided to the control.

If it is missing some data than adapt that datacontext to have it, even though this means reading that from a datacontext of its parent.

like image 138
Marino Šimić Avatar answered Oct 23 '22 12:10

Marino Šimić