4

I have an ItemsControl control that has an ObservableCollection as its ItemsSource. It also has a button located inside of its DataTemplate. The button's Command property is bound to a RelayCommand in the ViewModel (I'm using MVVM Light) and the CommandParameter is bound to the corresponding item in the ItemsSource.
The problem is that the command never fires, for some reason. Code-behind works fine, on the other hand. When debugging the mouse click event handler I can see that the sender (of type Button) has a CommandParameter filled with the correct data whereas Command is null.

What did I miss here?

XAML:

<ItemsControl 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 RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}, Path=DataContext.UserSelectedCommand}"
                    CommandParameter="{Binding}">
                    <!--...-->

ViewModel:

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)
{

}

3 Answers 3

23

Use named Element binding as binding source inside your data template to access commands from root data context. You can use root grid or other containers as named element. ItemsControl iteself can be used too.

<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}">
                    <!--...-->
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this and it worked - but I don't know why you have to set the ElementName to the name of the ItemsControl name, and why you have to specify Path=DataContext.FOO_COMMAND. Can anyone help to explain this?
7

You need to add "FindAncestor" to your relative source binding: RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}

Comments

-2

In my opinion you should change your strategy, put the command in the User class and from that class notify the view-model through an event. This is going to simplify your xaml code and, in my opinion, making your view-model more coherent.

2 Comments

That's a bad idea. User, which is a Model should not be handling commands.
In this case just wrap the user with a view-model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.