Jer*_*oen 23 wpf binding datatemplate itemtemplate viewmodel
我有多个视频显示它们与Mainviewmodel中的视频选择绑定.一切正常,直到我尝试将enter命令绑定到Mainviewmodel.我不知道这个的语法.目前,绑定设置为Video而不是Mainviewmodel.
错误信息:
'StartVideoCommand' property not found on 'object' ''Video'
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
Jer*_*oen 33
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.StartVideo}"
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用ElementNamebinding 而不是RelativeSource.
例子:
<Window x:Name="root" ... >
...
Command="{Binding ElementName=root, Path=DataContext.StartVideo}"
...
Run Code Online (Sandbox Code Playgroud)
一个可能的优势RelativeSource是这是明确的;如果有人更改了 XAML 层次结构,则相对引用可能会意外中断。(在这个绑定到 a 的特定示例中不太可能Window)。
此外,如果您的“根”元素已经被命名,那么它就更容易利用。
它也更具可读性。
| 归档时间: |
|
| 查看次数: |
11620 次 |
| 最近记录: |