C# WPF ItemsControl按钮命令绑定不工作

C# WPF ItemsControl按钮命令绑定不工作,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个ItemsControl控件,它有一个ObservableCollection作为其ItemsSource。它的DataTemplate中还有一个按钮。按钮的Command属性绑定到ViewModel中的RelayCommand(我使用的是MVVM Light),CommandParameter绑定到ItemsSource中的相应项。 问题是,出于某种原因,该命令从未发出。另一方面,代码隐藏可以很好地工作。调试鼠标单击事件处理程序时,我可以看到发送方(按钮类型)有一个CommandPar

我有一个ItemsControl控件,它有一个ObservableCollection作为其ItemsSource。它的DataTemplate中还有一个按钮。按钮的Command属性绑定到ViewModel中的RelayCommand(我使用的是MVVM Light),CommandParameter绑定到ItemsSource中的相应项。
问题是,出于某种原因,该命令从未发出。另一方面,代码隐藏可以很好地工作。调试鼠标单击事件处理程序时,我可以看到发送方(按钮类型)有一个CommandParameter,其中填充了正确的数据,而Command为null

我错过了什么

XAML:


视图模型:

private ObservableCollection<User> _users;
private RelayCommand<User> _userSelectedCommand;

public ObservableCollection<User> Users
{
    get { return _users; }
    set
    {
        _users = value;
        RaisePropertyChanged();
    }
}

public RelayCommand<User> UserSelectedCommand
{
    get { return _userSelectedCommand; }
}

protected override sealed void SetCommands() // called in the constructor which is in turned called by SimpleIoc
{
     userSelectedCommand = new RelayCommand<User>((user) => UserSeriesSelected(user));
}

private void UserSelected(User selectedUser)
{

}
private observeCollection\u用户;
专用中继命令_userSelectedCommand;
公共可观测收集用户
{
获取{return\u users;}
设置
{
_用户=价值;
RaisePropertyChanged();
}
}
public RelayCommand userselected命令
{
获取{return\u userSelectedCommand;}
}
protected override sealed void SetCommands()//在构造函数中调用,该构造函数反过来由SimpleIoc调用
{
userSelectedCommand=new RelayCommand((用户)=>UserSeriesSelected(用户));
}
private void UserSelected(用户选择用户)
{
}

在我看来,您应该更改策略,将命令放入
用户
类,并从该类通过事件通知视图模型。 这将简化您的xaml代码,并且在我看来,将使您的视图模型更加一致。

您需要在相对源绑定中添加“FindAncestor”:
RelativeSource={RelativeSourceFindAncestor,AncestorType={x:Type ItemsControl}}

使用命名元素绑定作为数据模板内的绑定源,从根数据上下文访问命令。您可以使用根网格或其他容器作为命名元素。也可以使用ItemsControl iteself

<ItemsControl x:Name="MyItems" ItemsSource="{Binding Users}"
              Margin="{StaticResource ContentMargin}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Margin="{StaticResource ImageButtonMargin}"
                    Style="{StaticResource ImageButtonStyle}"
                    Command="{Binding ElementName=MyItems,  Path=DataContext.UserSelectedCommand}"
                    CommandParameter="{Binding}">
                    <!--...-->


这是个坏主意<代码>用户,这是一个
模型
不应处理命令。在这种情况下,只需使用视图模型包装用户即可
<ItemsControl x:Name="MyItems" ItemsSource="{Binding Users}"
              Margin="{StaticResource ContentMargin}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button Margin="{StaticResource ImageButtonMargin}"
                    Style="{StaticResource ImageButtonStyle}"
                    Command="{Binding ElementName=MyItems,  Path=DataContext.UserSelectedCommand}"
                    CommandParameter="{Binding}">
                    <!--...-->