我有一个 ItemsControl,它的 ItemTemplate DataTemplate 包含一个按钮。我希望按钮上的命令绑定到 ItemsControl 的 DataContext 上的命令,而不是 ItemTemplate。我认为解决方案与使用RelativeSource有关,但到目前为止我的尝试都失败了:
<ItemsControl ItemsSource="{Binding Games}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding Path=GameSelectedCommand, Source={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"
CommandParameter="{Binding}"
Style="{StaticResource MenuButtonStyle}"
Content="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
如何让 Button 绑定到 ItemsControl 的 DataContext 对象的 GameSelectedCommand?