如何通过按钮将多个参数传递给WPF命令?

当前我的代码如下:

<Button Command="{Binding DrinkCommand}" commandparameter="Capuccino" Style="{StaticResource DrinkButton}">
    <TextBlock>Capuccino</TextBlock>
</Button>
<Button Command="{Binding DrinkWithSugarCommand}" commandparameter="Capuccino" Style="{StaticResource DrinkButton}">
    <TextBlock>Capuccino + sugar</TextBlock>
</Button>

您会发现对于含糖和不含糖的卡布其诺,我有不同的RelayCommand

我想添加添加额外牛奶的选项。但是比我会得到的:

DrinkCommand

DrinkWithSugarCommand

DrinkWithMilkCommand

DrinkWithSugarAndMilkCommand

有没有办法让DrinkCommand知道我想要加糖和/或牛奶的饮料(cappucino)?

ooygg 回答:如何通过按钮将多个参数传递给WPF命令?

您可以创建一个类来保存您的不同命令参数,并像这样使用它:

<Button Content="Capuccino + sugar" Command="{Binding DrinkCommand}">
    <Button.CommandParameter>
        <wpfapp1:MyCommandParameters Milk="false" Sugar="true"/>
    </Button.CommandParameter>
</Button>

您的课程看起来像

public class MyCommandParameters {
    public bool Milk { get; set; }
    public bool Sugar { get; set; }
}

,您可以在命令代码中使用它,并将其作为参数传递。

,

使用组合框选择命令类型和发送请求的按钮之后,它可以通过多种方式显示您的需求:

Xaml

    <Window x:Class="CommandNameSpace.Views.MainWindowView"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008">

        <Grid x:Name="MainGrid">
            <Canvas >
                <StackPanel VerticalAlignment="Center">
                    <ComboBox  ItemsSource="{Binding LisCommandType,Mode=TwoWay}"  SelectedItem="{Binding SelectedCommandType,Mode=TwoWay}" >
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Value}"/>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
                </StackPanel>

           <StackPanel VerticalAlignment="Center">
                    <Button x:Name="cmdCommand" IsDefault="True" Content="Commander" Command = "{Binding OrderCommand}" CommandParameter = "null"/>
           </StackPanel>
        </Canvas >
       </Grid>
    </Window >

主班

namespace CommandNameSpace
{
   public class MainCalss
   {
        public BaseCommand<string> OrderCommand { get; private set; }
        private Dictionary<string,string> _selectedCommandType = new Dictionary<string,string>();
        private KeyValuePair<string,string> _selectedLanguage;

        public ServicesControlViewModel()
        {
            OrderCommand = new BaseCommand<string>(cmdOrderCommand);

            LisCommandType.Add("DrinkCommand","Drink");
            LisCommandType.Add("DrinkWithSugarCommand","Drink With Sugar");
            LisCommandType.Add("DrinkWithMilkCommand","Drink With Milk");
            LisCommandType.Add("DrinkWithSugarAndMilkCommand","Drin kWith Sugar And Milk");

            SelectedCommandType = new KeyValuePair<string,string>("DrinkCommand","Drink");
        }

        public Dictionary<string,string> LisCommandType
        {
            get
            {
            return _liscommandType;
            }
            set
            {
            _liscommandType = value;
            }
        }

        public KeyValuePair<string,string> SelectedCommandType
        {
            get
            {
            return _selectedCommandType;
            }
            set
            {
            _selectedCommandType = value;
            }
        }

        private void cmdOrderCommand(string paramerter)
        {
            switch (SelectedCommandType)
            {
                case "DrinkCommand":
                    Instruction for DrinkCommand type

                    break;
                case "DrinkWithSugarCommand":
                    Instruction for DrinkCommand type

                    break;
                case "DrinkWithMilkCommand":
                    Instruction for DrinkCommand type

                    break;
                case "DrinkWithSugarAndMilkCommand":
                    Instruction for DrinkCommand type

                    break;
            }
        }
}

号召性用语

MainCalss MainCl = new MainCalss();
MainWindowView MainV = new MainWindowView();
MainV.Datacontext = MainCl;
MainV.ShowDialog(); 

Cordialy

,

您可以使用带有单个参数的单个命令,该参数可以在命令方法中进行解析。

这种参数可以是枚举类型,它用Flags属性声明:

document.getElementById('option').value= datalist;

xaml:

[Flags]
private enum Tastes
{
    None = 0,Sugar = 1,Milk = 1 << 1,Capuccino = 1 << 2
}

命令方法:

<Button Command="{Binding DrinkCommand}" CommandParameter="Capuccino" 
        Content="Capuccino" Style="{StaticResource DrinkButton}"/>
<Button Command="{Binding DrinkCommand}" CommandParameter="Capuccino,Sugar"
        Content="Capuccino + sugar" Style="{StaticResource DrinkButton}"/>
<Button Command="{Binding DrinkCommand}" CommandParameter="Milk" 
        Content="Milk" Style="{StaticResource DrinkButton}"/>
<Button Command="{Binding DrinkCommand}" CommandParameter="Milk,Sugar" 
        Content="Milk + sugar" Style="{StaticResource DrinkButton}"/>
本文链接:https://www.f2er.com/3160079.html

大家都在问