相关疑难解决方法(0)

从ItemTemplate内部绑定到ItemsControl的DataContext

我有一个ItemsControl,其ItemTemplate DataTemplate包含一个Button.我希望按钮上的Command绑定到ItemsControl的DataContext上的Command,而不是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>
Run Code Online (Sandbox Code Playgroud)

如何让Button绑定到ItemsControl的DataContext对象的GameSelectedCommand?

data-binding wpf xaml datatemplate itemscontrol

23
推荐指数
1
解决办法
2万
查看次数

标签 统计

data-binding ×1

datatemplate ×1

itemscontrol ×1

wpf ×1

xaml ×1