简体   繁体   English

如何确定哪个用户控件正在调用命令

[英]How to determine which user control is calling command

Relative new to MVVM - I realize this is simple question but can't seem to find the answer. MVVM 相对较新 - 我意识到这是一个简单的问题,但似乎无法找到答案。

I have 4 grouped radio buttons that when one is selected will show its associated options. I have 4 grouped radio buttons that when one is selected will show its associated options. I assume these 4 radio buttons should be linked to the same viewmodel command which in this case is called UpdateIndex .我假设这 4 个单选按钮应该链接到同一个 viewmodel 命令,在这种情况下称为UpdateIndex

How do I determine which of the radio buttons is calling the UpdateIndex so that I can take appropriate action and show the appropriate options?如何确定哪个单选按钮正在调用UpdateIndex以便我可以采取适当的操作并显示适当的选项?

Note that my UpdateIndex and UpdateIndexExecute does get called correctly from my radio button command binding, I just don't know how to determine which radio button is calling it.请注意,我的UpdateIndexUpdateIndexExecute确实从我的单选按钮命令绑定中正确调用,我只是不知道如何确定哪个单选按钮正在调用它。 I assume it has to do with CommandParameter - but not sure how to access it from the viewmodel.我认为它与CommandParameter - 但不确定如何从视图模型访问它。

An example of my radio button:我的单选按钮的一个例子:

<RadioButton
    Content="Option 1"
    GroupName="GroupHeader"
    Command="{Binding UpdateIndex}" />

Code snippet of my command being called from the radio button when clicked...单击时从单选按钮调用我的命令的代码片段...

void UpdateIndexExecute()
{

}

bool CanUpdateIndex()
{
   return true;
}

public ICommand UpdateIndex
{
    get
    {
        return new RelayCommand(UpdateTabIndexExecute, CanUpdateTabIndex);
    }
}

In a true MVVM implementation, you won't know which RadioButton executed the command, because the ViewModel should not have any information about the View.在真正的 MVVM 实现中,您不会知道是哪个 RadioButton 执行了该命令,因为 ViewModel 不应该有关于 View 的任何信息。 User Controls fall squarely in the category of "things that only exist within the View, not the ViewModel."用户控件完全属于“仅存在于视图中,而不存在于视图模型中的事物”的类别。 You should instead pass something meaningful to the ViewModel.相反,您应该向 ViewModel 传递一些有意义的东西。

You are correct there are ways to pass information into an ICommand using the "CommandParameter" of a Command Binding.您是对的,有多种方法可以使用命令绑定的“CommandParameter”将信息传递到 ICommand。 For that, you would use the "generic" form of the RelayCommand ( RelayCommand ) class, where "T" represents the type of object you are passing as a parameter.为此,您将使用 RelayCommand ( RelayCommand ) 类的“通用”形式,其中“T”代表您作为参数传递的对象类型。

If you're just trying to pass an index as a parameter, I imagine it'll look something like this:如果您只是想将索引作为参数传递,我想它看起来像这样:

<!-- We are passing index "1" as a parameter -->
<RadioButton Content="Option 1"  GroupName="GroupHeader" 
             Command="{Binding UpdateIndex, CommandParameter=1}"/> 

Then in your ViewModel:然后在您的 ViewModel 中:

void UpdateIndexExecute(int index)
{

}
    
bool CanUpdateIndex(int index)
{
    return true;
}

public ICommand UpdateIndex 
{ 
    get 
    { 
        return new RelayCommand<int>(UpdateTabIndexExecute, CanUpdateTabIndex); 
    } 
}

您可以绑定内容,而不是绑定命令,使用 INotifyPropertyChanged 接口来处理控件所做的更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM