从绑定一个DataTemplate内到视图模型 [英] Binding to viewmodel from inside a datatemplate

查看:118
本文介绍了从绑定一个DataTemplate内到视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个视频显示,他们都与一个Mainviewmodel约束videocollection。一切正常,直到我试图绑定输入命令Mainviewmodel。我不知道这样做的语法。因为它代表设置为视频而不是Mainviewmodel绑定。

ERRORMESSAGE:

 在'对象'''视频'找不到'StartVideoCommand属性

XAML中:

 < Window.Resources>
  <局部:MainViewModel X:键=MainViewModel/>
< /Window.Resources>
  <电网的DataContext ={StaticResource的MainViewModel}>
    < ListBox中的ItemsSource ={结合影片}>
      < ListBox.ItemTemplate>
        <&DataTemplate的GT;
          <网格和GT;
            < Grid.InputBindings>! <键绑定键=输入command ={结合StartVideo}/> !绑定到视频不Mainviewmodel哎呀            < /Grid.InputBindings>
             ...布局的东西
              < TextBlock的文本={结合标题}Grid.Column =0Grid.Row =0前景=白/>
              < TextBlock的文本={结合日期}Grid.Column =0Grid.Row =1前景=白的Horizo​​ntalAlignment =左/>
              < TextBlock的文本={结合长度}Grid.Column =1Grid.Row =1前景=白的Horizo​​ntalAlignment =右/>
             ...关闭标签


解决方案

 命令={绑定的RelativeSource = {的RelativeSource AncestorType = {X:类型窗口}},路径= DataContext.StartVideo }

I have multiple videos displayed they are bound with a videocollection in Mainviewmodel. Everything works fine untill I try to bind the enter command to Mainviewmodel. I Don't know the syntax for this. As it stands the binding is set to Video and not Mainviewmodel.

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

解决方案

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

这篇关于从绑定一个DataTemplate内到视图模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆