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>
c # wpf xaml
Pavlik
source share