Xaml 如何在silverlight4中调用listbox内部的命令

Xaml 如何在silverlight4中调用listbox内部的命令,xaml,data-binding,silverlight-4.0,Xaml,Data Binding,Silverlight 4.0,我正在使用Listbox,它包含按钮,我想使用command处理按钮单击事件。但我的命令从不调用 这是正确的方法吗 <pmControls:pmListBox Grid.Row="1" Margin="3" ItemsSource="{Binding Countries}" SelectedItem="{Binding SelectedCountry}" > <pmControls:pmListBox.ItemTemplate >

我正在使用Listbox,它包含按钮,我想使用command处理按钮单击事件。但我的命令从不调用

这是正确的方法吗

  <pmControls:pmListBox Grid.Row="1" Margin="3" ItemsSource="{Binding Countries}" SelectedItem="{Binding SelectedCountry}" >
                <pmControls:pmListBox.ItemTemplate >
                    <DataTemplate >
                        <Button Command="{Binding GetAllStatesCommand}"   CommandParameter="{Binding}" Margin="3" Width="100" Height="50" Content="{Binding Title}">                                                                                                                                
                        </Button>

                    </DataTemplate>
                </pmControls:pmListBox.ItemTemplate>
  </pmControls:pmListBox>

一个列表项的
DataContext
与周围控件的
DataContext
不同。要将该命令绑定到该控件的
DataContext
,有两个选项:

您可以为控件提供名称和对该名称的引用:

<pmControls:pmListBox x:Name="myCoolListBox" [...]>
    <pmControls:pmListBox.ItemTemplate>
        <DataTemplate>
            <Button Command="{Binding DataContext.GetAllStatesCommand, ElementName=myCoolListBox}" CommandParameter="{Binding}" [...] />                                           
        </DataTemplate>
    </pmControls:pmListBox.ItemTemplate>
</pmControls:pmListBox>
…并在
列表框的参考资料部分创建一个实例:

<pmControls:pmListBox x:Name="myCoolListBox" [...]>
    <pmControls:pmListBox.Resources>
        <local:DataContextBinder x:Key="dataContextBinder" Context="{Binding}" />
    </pmControls:pmListBox.Resources>
    <pmControls:pmListBox.ItemTemplate>
        <DataTemplate>
            <Button Command="{Binding Context.GetAllStatesCommand, Source={StaticResource dataContextBinder}" CommandParameter="{Binding}" [...] />                                           
        </DataTemplate>
    </pmControls:pmListBox.ItemTemplate>
</pmControls:pmListBox>


谢谢,它可以工作,但现在我的命令调用了,但它再次释放了我给listbox:ItemsSource=“{binding Countries}”SelectedItem=“{binding SelectedCountry}”的先前绑定我没有得到选中的对象??请看我修改过的答案-如果你绑定id,你当然只能得到作为命令参数的项。无论你在我的代码中找到什么
[…]
,我都给你留下了一些你的,以提高可读性。。。