wpf - 从数据模板内部绑定(bind)到 View 模型

标签 wpf binding viewmodel datatemplate itemtemplate

我显示了多个视频,它们与 Mainviewmodel 中的视频集合绑定(bind)。一切正常,直到我尝试将输入命令绑定(bind)到 Mainviewmodel。我不知道这个的语法。就目前而言,绑定(bind)设置为 Video 而不是 Mainviewmodel。

错误信息:

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

xml:
<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

最佳答案

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

关于wpf - 从数据模板内部绑定(bind)到 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18245936/

相关文章:

c# - 打开Outlook后无法创建带有Outlook Interop的电子邮件

wpf - 问题绑定(bind) DataGridComboBoxColumn.ItemsSource

WPF 形状矩形绑定(bind)

c# - 当你拥有 Rich Models 时你会做什么?

javascript - 弹窗使用knockout js

wpf - 类项目中的 ViewModel

c# - 打开和关闭绑定(bind)

c - WPF 中的 SelectedRow 和 DataBoundItem 是如何获取的?

c# - 暂停 Kinect 相机 - SDK 重新保护事件处理程序时可能出错

WPF - 过滤/搜索 TreeView 中的多个 Collection View