WPF:如何通过XAML将整个Control作为CommandParameter传递?

P A*_*I K 8 c# wpf xaml

我正在使用MVVM,ViewModel层提供自定义ICommand对象.一个ViewModel对象可以通过DataContext属性连接到许多View对象(窗口,页面等).在ICommand.CanExecute()中,我想检查View中某些控件的缺省验证错误(附加到ViewModel道具,对于特定的VM命令很重要).一个ViewModel可以提供许多命令,每个命令都有自己的一组控件用于错误验证验证.所以,伪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>
Run Code Online (Sandbox Code Playgroud)

第二个问题是特定命令可能由控件调用,控件本身是DataTemplate for collection项的一部分(在我的例子中是ListBoxItem数据模板的一部分).我的模板化列表框项目有两个文本框(绑定到相应ViewModel的两个道具)和按钮,它们调用ViewModel命令.因此,在命令CanExecute()中,我需要检查某些窗口控件和两个文本框的验证错误,这两个文本框属于此列表项,而不是其他项.如果我想将ListBoxItem.IsSelected属性作为CommandParameter传递,下面的代码工作正常:

<Button DataContext="{Binding}" 
        Command="{Binding Path=SwitchCommand}"
        CommandParameter="{Binding Path=IsSelected, RelativeSource={
                                   RelativeSource
                                   Mode=FindAncestor,
                                   AncestorType={x:Type ListBoxItem}}}"/>
Run Code Online (Sandbox Code Playgroud)

但是如何将整个(DependencyObject)ListBoxItem作为CommandParameter传递?以及如何通过{Binding RelativeSource}传递的ListBoxItem可以与第一个代码示例中的其他当前窗口控件混合使用?


我很抱歉,但是如何在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>
Run Code Online (Sandbox Code Playgroud)

Tho*_*que 28

只需使用没有的绑定Path:

<Button DataContext="{Binding}" 
        Command="{Binding Path=SwitchCommand}"
        CommandParameter="{Binding RelativeSource=
                                   {RelativeSource
                                    Mode=FindAncestor,
                                    AncestorType={x:Type ListBoxItem}}}"/>
Run Code Online (Sandbox Code Playgroud)