Binding to viewmodel from inside a datatemplate Binding to viewmodel from inside a datatemplate wpf wpf

Binding to viewmodel from inside a datatemplate


Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.StartVideo}"


Another approach would be to use ElementName binding instead of RelativeSource.

Example:

<Window x:Name="root" ... >  ...  Command="{Binding ElementName=root, Path=DataContext.StartVideo}"  ...

A possible advantage over RelativeSource is that this is explicit; if someone changes the XAML hierarchy then relative references could break unintentionally. (Not likely in this specific example of binding to a Window however).

Also if your "root" element already is named then so much the better it is easy to take advantage of.

It is also somewhat more readable.