我有一个ItemsControl,其中ItemTemplate DataTemplate包含一个按钮。我希望按钮上的Command绑定到ItemsControl的DataContext上的Command,而不是ItemTemplate上的Command。我认为解决方案涉及使用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>
我该如何让按钮绑定到ItemsControl的DataContext对象的GameSelectedCommand命令?
DataContext.
。我以前也经常犯这个错误 :) - Simon_Weaver