开发者

Firing RelayCommand from CodeBehind bound to VM

开发者 https://www.devze.com 2023-01-24 01:02 出处:网络
I wonder if I can create a RelayCommand on my ViewModel like this: public RelayCommand<IList<VectorViewModel>> MyCommand { get; set; }

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


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


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://stackoverflow.com/questions/636383/wpf-ways-to-find-controls

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号