Using the parent's DataContext (WPF - Dynamic Menu Command Binding)

43,559

Solution 1

If you want a dirty, MVVM-breaking solution, then set the Tag="{Binding}" on the button and handle the Click event. In the event handler, call the command on your ViewModel.

Solution 2

Use the binding below for your button's command:

{Binding DataContext.CommandName, 
         RelativeSource={RelativeSource FindAncestor, 
                         AncestorType={x:Type MyUserControl}}}

This will tell it to find your UserControl and use its DataContext.

Solution 3

Ok, then what about modifying your data item class so that it has a property referencing to the whole model view?

If your ItemsSource is of type ObservableCollection<DataItem> then modify DataItem type like this:

public class DataItem
{
    public BusinessObject Value { get; set; }

    private ModelView modelView;

    public ModelView ModelView
    {
        get
        {
            return modelView;
        }
    }

    public DataItem(ModelView modelView)
    {
        this.modelView = modelView;
    }
}

Solution 4

RelativeSource works, but I don't think it's right to let controls to prowl across each other's properties. It is strange that button placed inside an item view does something with an outer data source rather than with the bound item. You might need to review the program code’s design.

Share:
43,559
Jesus Rodriguez
Author by

Jesus Rodriguez

Angular.js developer seeking to learn as much as possible. Also an avid blogger :)

Updated on October 26, 2020

Comments

  • Jesus Rodriguez
    Jesus Rodriguez over 3 years

    I looked over this web and google and the solutions didn't work for me.

    I have a command on the ViewModel of a UserControl. Well, The usercontrol have a ItemsControl binded to a ObservableCollection. Inside the DataTemplate of the ItemsControl.ItemTemplate I have a button and I want to use the command. I can't bind the command because inside the DataTemplate, the datacontext is not the ViewModel but an item of the ObservableCollection.

    The question is: How can I bind the button to the command if a lost the parent datacontext?

    I think that this need to have an easy solution because I think that this is a common problem.

    Imagine this sceneario:

    You have a ListBox item with an observableCollection as the ItemsSource, so you are using a datatemplate inside the ListBox for every element in the collection. Well, you want to delete the selected item and you put a button in every row for that job. ¿How do you do that?

    In MVP, I can do this in the click event of the button:

    Button but = e.Source as Button;
    
    if (but != null)
          Presenter.ActualNote = but.DataContext as Note;
    

    In short. You send the datacontext of the row (the selected item) to the presenter.

    But, how can I do it in the mvvm way? Because I need to use a command but I can't assign the command to the button because the button does know nothing about the ViewModel (where the command exists).

    As you can see, the button has to exist inside the datatemplate, then the datacontext is not the ViewModel anymore.... There is why I need to access to the parent's DataContext, for access to the command.

    I hope that you understand my problem better.

    Thank you.