Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVVM/MVVM灯中绑定单选按钮的正确方法是什么_C#_Wpf_Mvvm_Mvvm Light - Fatal编程技术网

C# 在MVVM/MVVM灯中绑定单选按钮的正确方法是什么

C# 在MVVM/MVVM灯中绑定单选按钮的正确方法是什么,c#,wpf,mvvm,mvvm-light,C#,Wpf,Mvvm,Mvvm Light,我目前正在单独绑定我的单选按钮,换句话说,我对每个按钮都有一个命令,一切都很好,但我希望只有一个命令可以绑定所有按钮 这是我所拥有的 XAML: <RadioButton x:Name="radioButton1" Content="RadioButton1" IsChecked="True" Command="{Binding Rad

我目前正在单独绑定我的单选按钮,换句话说,我对每个按钮都有一个
命令
,一切都很好,但我希望只有一个命令可以绑定所有按钮

这是我所拥有的

XAML:

<RadioButton x:Name="radioButton1" 
                         Content="RadioButton1"  
                         IsChecked="True"
                         Command="{Binding RadioButton1Command }"/>
<RadioButton x:Name="radioButton2" 
                         Content="RadioButton2"  
                         IsChecked="True"
                         Command="{Binding RadioButton2Command }"/>
    public RelayCommand RadioButton1Command { get; }
    public RelayCommand RadioButton2Command { get; }

    public MyClassConstructorViewModel()
    {
        RadioButton1Command = new RelayCommand(radioButton1Click);
        RadioButton1Command = new RelayCommand(radioButton2Click);
    }


    private void radioButton1Click()
    {
        Console.WriteLine("Radio Button 1 Clicked...");
    }
    private void radioButton2Click()
    {
        Console.WriteLine("Radio Button 2 Clicked...");
    }
是否有办法将所有单选按钮绑定到一个
RelayCommand
并能够相应地响应


谢谢

您可以绑定
单选按钮的名称。根据方法中收到的名称,您可以选择正确的操作

XAML

<RadioButton x:Name="radioButton1" 
                         Content="RadioButton1"  
                         IsChecked="True"
                         Command="{Binding RadioButtonCommand }" CommandParameter="{Binding Path=Name, RelativeSource={RelativeSource Self}}"/>

<RadioButton x:Name="radioButton2" 
                         Content="RadioButton2"  
                         IsChecked="True"
                         Command="{Binding RadioButtonCommand }" CommandParameter="{Binding Path=Name, RelativeSource={RelativeSource Self}}"/>

视图模型

public RelayCommand<string> RadioButtonCommand { get; }

public MyClassConstructorViewModel()
{
    RadioButtonCommand = new RelayCommand<string>(radioButtonClick);
}


private void radioButtonClick(string name)
{
    if(name == "radioButton1")
        Console.WriteLine("Radio Button 1 Clicked...");
    else if(name == "radioButton2")
        Console.WriteLine("Radio Button 2 Clicked...");
}
public RelayCommand无线电按钮命令{get;}
公共MyClassConstructorViewModel()
{
RadioButtonCommand=新的中继命令(radioButtonClick);
}
专用void radioButtonClick(字符串名称)
{
如果(名称==“radioButton1”)
Console.WriteLine(“单击单选按钮1…”);
否则如果(名称==“radioButton2”)
Console.WriteLine(“单击单选按钮2…”);
}

传递一些CommandParameter,对于每个RadioButtonGlad都是唯一的,以帮助您:)但是,您应该尝试为CommandParameter找到一些更通用的值。视图模型中不应知道视图元素的名称。请注意,您可以分配一个字符串,例如
CommandParameter=“1”
。这不是必需的。只需为CommandParameter分配一个唯一的属性。该绑定需要视图模型中的属性
SomeString
。@fs\u tigre您只需通过文本字符串设置
CommandParameter
,如
CommandParameter
=1