2013-08-15 13 views
19

Ho più video visualizzati che sono associati a una videocollection in Mainviewmodel. Tutto funziona bene fino a quando cerco di associare il comando di invio a Mainviewmodel. Non conosco la sintassi per questo. Così com'è, il binding è impostato su Video e non su Mainviewmodel.Collegamento a viewmodel dall'interno di un datatemplate

errorMessage:

'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 

risposta

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

ho dovuto legarsi a un comando quindi ho fatto: 'Command =" {Binding RelativeSource = {RelativeSource AncestorType = {x: Digitare UserControl}}, Path = DataContext.COMMAND_I_WANT_TO_BIND_TO} "' –

Problemi correlati