Firing RelayCommand from CodeBehind bound to VM
Asked Answered
S

2

9

I wonder if I can create a RelayCommand on my ViewModel like this:

public RelayCommand<IList<VectorViewModel>> MyCommand { get; set; }

ctor:

MyCommand = new RelayCommand<IList<VectorViewModel>>(DoSomething);

And from the Code behind XAML, I get the selected rows from a DataGrid and put them into a List.

if (xamDatagridVector.SelectedItems.Records.Count >= 3)
{
                var list = new List<VectorViewModel>();
                foreach (DataRecord record in xamDatagridVector.SelectedItems.Records)
                {
                    list.Add((VectorViewModel)record.DataItem);
                }
}

At this stage I would like to send the List back to the ViewModel by using that RelayCommand I had created earlier. Would that be possible to create a RelayCommand in code and Bind it to the ViewModel's command and fire it off?

What alternative way is there? I could of course use the weak-referenced Messenger class in MVVM-Light, but something I dont like there is that it will send it to all subscribers of that call, and not only the underlying ViewModel (Its deadly using Messenger when you have several instances of the same View within TabControls)

I hope someone has an idea to keep me going, Many Thanks, Kave

Secunderabad answered 9/11, 2010 at 17:47 Comment(0)
P
22

Just call the Execute method of the command after checking the result of CanExecute:

var viewModel = (MyViewModel)DataContext;
if (viewModel.MyCommand.CanExecute(list))
    viewModel.MyCommand.Execute(list);
Polanco answered 9/11, 2010 at 18:21 Comment(0)
D
0

and if sometimes the DataContext of a ui element differs from the form as a whole, as I encountered, then you can do something like this:

 private void TextBoxTextChanged(object sender, TextChangedEventArgs e)
    {
        var binding = ((TextBox)sender).GetBindingExpression(TextBox.TextProperty);
        binding.UpdateSource();

        var msg = String.Format("Migrator file selection updated to {0}", ((TextBox)sender).Text);
        var rowControl = UiHelpers.FindVisualParent<UserControl>((DependencyObject)sender); // get the     FileNameSettingsRow UserControl
        var form = UiHelpers.FindVisualParent<UserControl>((DependencyObject)rowControl);  // get the main form it is used on

        var viewModel = (UseCaseSettingsViewModel)form.DataContext;

        if (viewModel.UpdateFileInCollectionCommand.CanExecute(((TextBox)sender).Text))
            viewModel.UpdateFileInCollectionCommand.Execute(((TextBox)sender).Text);


        Messenger.Default.Send(new NotificationMessage(this, msg), Notifications.AppendSysMessageTextToken);

        // Tell the UseCaseSettingsViewModel to force an update and reload
        //Messenger.Default.Send(new NotificationMessage(this, ((TextBox)sender).Text), Notifications.FileSelectionChangedInternalToken);
    }

which will find the DataContext of the UserControl on which the subordinate UserControl exists, then go ahead and do fun things as indicated in the earlier answers. In this case editing a text box on the subordinate user control needed to tell the overarching viewmodel that the text had changed.

Note that FindVisualParent comes from here >>> https://mcmap.net/q/99737/-how-can-i-find-wpf-controls-by-name-or-type

Dihybrid answered 1/8, 2011 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.