WPF: How to pass all control as CommandParameter through XAML?

I use MVVM, and custom ICommand objects are provided using the ViewModel level. One ViewModel can be attached at the same time through the DataContext property to many View objects (windows, pages, etc.). In ICommand.CanExecute (), I want to verify that there are no validation errors for some controls in the view (which are tied to the ViewModel details that are relevant to a particular VM command). One ViewModel can provide many commands, each of which has its own set of controls for checking the validity of errors. So pseudo-XAML:

<Button.CommandParameter> <x:Array Type="sys_win:DependencyObject"> <sys_win:DependencyObject> <reference_to_textbox_or_other_control/> </sys_win:DependencyObject> <sys_win:DependencyObject> <reference_to_textbox_or_other_control/> </sys_win:DependencyObject> </x:Array> </Button.CommandParameter> 

The second problem is that a particular command can be called by a control, which itself is part of the DataTemplate for the collection element (in my case, part of the ListBoxItem data template). My list template element has two text fields (tied to two details of the corresponding ViewModel) and buttons that invoke the ViewModel command. So, in the CanExecute () command, I need to check for validation errors for some window controls and two text fields that belong to this list, and not to other elements. The code below works fine if I want to pass the ListBoxItem.IsSelected property as a CommandParameter parameter:

 <Button DataContext="{Binding}" Command="{Binding Path=SwitchCommand}" CommandParameter="{Binding Path=IsSelected, RelativeSource={ RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}"/> 

But how can I pass the ListBoxItem of the target (DependencyObject) as a CommandParameter? And how can this ListBoxItem passed through {Binding RelativeSource} be mixed with other current controls in the first code sample?


I'm sorry, but how to add links to controls in xaml?

 <Button.CommandParameter> <x:Array Type="sys_win:DependencyObject"> <sys_win:DependencyObject> <reference_to_textbox_or_other_control/> </sys_win:DependencyObject> <sys_win:DependencyObject> <reference_to_textbox_or_other_control/> </sys_win:DependencyObject> </x:Array> </Button.CommandParameter> 
+7
c # wpf xaml
source share
2 answers

Just use binding without Path :

 <Button DataContext="{Binding}" Command="{Binding Path=SwitchCommand}" CommandParameter="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}"/> 
+23
source share

I'm not sure if I am reading your example correctly, but it seems to violate the MVVM principle. (My apologies if I read it incorrectly).

The idea behind MVVM is to separate the view model from any dependency on the XAML / View object. You break this if CommandParameter is dependent on user control. I would like to create state properties in the ViewModel and bind user checks to these states, and then in CanExecute you can check the values ​​of these properties, rather than trying to bind to usercontrol.

+1
source share

All Articles