2013-08-15 14 views
19

Ich habe mehrere Videos angezeigt, die mit einer Videokollektion in Mainviewmodel gebunden sind. Alles funktioniert einwandfrei, bis ich versuche, den Enter-Befehl an Mainviewmodel zu binden. Ich kenne die Syntax dafür nicht. Wie es steht, ist die Bindung auf Video und nicht Mainviewmodel festgelegt.Binding to viewmodel aus einer Datatemplate

Fehlermeldung:

'StartVideoCommand' property not found on 'object' ''Video' 

XAML:

<Window.Resources> 
    <local:MainViewModel x:Key="MainViewModel"/> 
</Window.Resources> 
    <Grid DataContext="{StaticResource MainViewModel}"> 
    <ListBox ItemsSource="{Binding Videos}"> 
     <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
      <Grid.InputBindings> 

!!!   <KeyBinding Key="Enter" Command="{Binding StartVideo}" /> !Bound to Video not to Mainviewmodel grrr 

      </Grid.InputBindings> 
      ... layout stuff 
       <TextBlock Text="{Binding Title}" Grid.Column="0" Grid.Row="0" Foreground="White"/> 
       <TextBlock Text="{Binding Date}" Grid.Column="0" Grid.Row="1" Foreground="White" HorizontalAlignment="Left"/> 
       <TextBlock Text="{Binding Length}" Grid.Column="1" Grid.Row="1" Foreground="White" HorizontalAlignment="Right"/> 
      ... closing tags 

Antwort

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

Ich hatte daher auf einen Befehl zu binden, ich tat: 'Command =" {Binding Relative = {Relative AncestorType = {x: Geben Sie UserControl ein}, Pfad = DataContext.COMMAND_I_WANT_TO_BIND_TO} "' –