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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With