Wpf 将命令绑定到ItemsControl内的按钮

Wpf 将命令绑定到ItemsControl内的按钮,wpf,xaml,data-binding,Wpf,Xaml,Data Binding,我的看法 我想为按钮绑定testCommand 提前感谢。使用相对资源绑定访问项控件的数据上下文(您的视图模型) <ItemsControl ItemsSource="{Binding pagination.PageList}"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal

我的看法


我想为按钮绑定
testCommand


提前感谢。

使用
相对资源
绑定访问
项控件的
数据上下文
(您的视图模型)

<ItemsControl ItemsSource="{Binding pagination.PageList}">
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal" Margin="0">
            <Button Content="{Binding}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
                    **Command="{Binding Path=testCommand}"**
                    Width="20" Margin="10,0"></Button>
         </StackPanel>
      </DataTemplate>
   </ItemsControl.ItemTemplate>
   <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel Orientation="Horizontal"/>
      </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
</ItemsControl>


哪里是
testCommand
?是否在视图模型中的数据项中也包含
分页。PageList
?我的视图模型具有分页和testcommand它的工作方式非常好!
<Button Content="{Binding}"
        CommandParameter="{Binding RelativeSource={RelativeSource Self}}"
        Command="{Binding DataContext.testCommand, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
        Width="20"
        Margin="10,0"/>