本文介绍了WPF:如何通过XAML传递整个控制为CommandParameter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVVM和视图模型层提供的自定义ICommand的对象。同时一个视图模型对象可以经由DataContext属性附加到许多视图对象(窗口,网页等)。在ICommand.CanExecute()我想查无验证错误的在浏览某些控件(其连接到视图模型道具,特定VM命令显著)。一个视图模型可以提供许多的命令,他们每个人都有自己的错误验证验证控件集。因此,伪XAML是:



 < Button.CommandParameter> 
< X:数组类型=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:数组>
< /Button.CommandParameter>



的第二个问题是,特定的命令可以由控制被调用,它本身是的部分DataTemplate中收集的项目(在我的情况 - ListBoxItem的数据模板的一部分)。我的模板列表框项目有两个文本框(绑定到对应的视图模型两个道具)和按钮,它调用的ViewModel命令。所以,在命令CanExecute()我需要检查一些窗口控件和放大器验证错误;两个文本框,属于这个列表项,而不是其他的物品。下面的代码工作正常,如果我想ListBoxItem.IsSelected财产作为传递CommandParameter:

 <按钮的DataContext ={结合} 
命令={绑定路径= switch命令}
CommandParameter ={绑定路径= IsSelected,的RelativeSource = {
的RelativeSource
模式= FindAncestor,
AncestorType = {X:输入一个ListBoxItem}}}/>



但我怎么能全通(DependencyObject的)ListBoxItem的作为CommandParameter?以及如何ListBoxItem中,通过{绑定的RelativeSource}传递可以与其他当前窗口控件中的第一个代码示例混合?






我很抱歉,但我怎么能在XAML中引用添加到控制

 < Button.CommandParameter> 
< X:数组类型=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:数组>
< /Button.CommandParameter>


解决方案

只需使用不带路径:

 <按钮的DataContext ={结合}
命令= {结合路径= switch命令}
CommandParameter ={绑定的RelativeSource =
{的RelativeSource
模式= FindAncestor,
AncestorType = {X:输入一个ListBoxItem}}}/> ;


I'm using MVVM and custom ICommand objects are provided by ViewModel layer. One ViewModel object at the same time can be attached via DataContext property to many View objects (windows, pages, etc). In ICommand.CanExecute() I want to check absence of validation errors for some controls in View (which attached to ViewModel props, significant for a particular VM command). One ViewModel can provide many commands, each of them has own set of controls for errors validation verification. So, pseudo-XAML is:

<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 the particular command may be invoked by control, which itself is the part of the DataTemplate for collection item (in my case - part of ListBoxItem data template). My templated listbox item has two text boxes (binded to two props of corresponding ViewModel) and button, which invoke the ViewModel command. So, in command CanExecute() I need to check for validation errors for some window controls & two text boxes, which belongs to this listitem, not other items. The code below works fine if I want to pass ListBoxItem.IsSelected property as CommandParameter:

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

But how can I pass whole (DependencyObject)ListBoxItem as the CommandParameter? And how this ListBoxItem, passed via {Binding RelativeSource} can be mixed with other current window controls in the first code example?


I'm very sorry, but how can I add the references 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>
解决方案

Just use a binding with no Path :

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

这篇关于WPF:如何通过XAML传递整个控制为CommandParameter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 23:43