¿Cómo pasar un UserControl con CommandParameter?

I have using a DelegateCommand and i want to pass the UserControl with Command.

#region OpenViewCommand
private DelegateCommand<UserControl> _openViewCommand;
public ICommand OpenViewCommand
        if (_openViewCommand == null)
            _openViewCommand = new DelegateCommand<UserControl>(new Action<UserControl>(OpenView));
        return _openViewCommand;

public void OpenView(UserControl ViewName)
    UserControl ctrl = (UserControl)ViewName;

Command in XAML

<Button Name="btnStockGroups" Command="{Binding OpenViewCommand}" CommandParameter="JIMS.View.Stock.StockGroups">stock group</Button>

preguntado el 03 de mayo de 12 a las 21:05

Why do you need access to the control in your ViewModel? The general MVVM appraoch would be to bind your control to the ViewModel and read data from the properties that it is bound to. -

Is the OpenView command intended to cause a particular type of UserControl to be instantiated and shown to the user? -

1 Respuestas

If you give your UserControl an x:Name (e.g. "MyView"), you should be able to do something like this:

<Button Name="btnStockGroups" 
        Command="{Binding OpenViewCommand}" 
        CommandParameter="{Binding ElementName=MyView}">

contestado el 03 de mayo de 12 a las 21:05

the element is not in the window.. its a UserControl file - Kishore Kumar

How can you do it without naming the element? - Alex Hope O'Connor

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.