Bind to Window view model property when its not the data context
-
13-11-2019 - |
Domanda
The current data context is SomeViewModel but I need to bind to a property on the view model of the main window. Im guessing I need to specify Source or RelativeSource in the binding but not quite sure how to manage that. The MainWindowViewModel is the data context of the main window (and is also a public member). Any guidance most welcome!
Soluzione
Try something like
{Binding Path=DataContext.YourMainWindowViewModelPropertyName,
RelativeSource={RelativeSource AncestorType={x:Type Window}}}
It should work if your main window is instance of Window type, and your current "object" is sub-UserControl.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow