1

Currently my code looks like:

<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>

You can see that I have a different RelayCommand for a capuccino with sugar and without sugar.

I would like to add the option to add extra milk. However than I will get:

DrinkCommand,

DrinkWithSugarCommand,

DrinkWithMilkCommand,

DrinkWithSugarAndMilkCommand.

Is there a way to let the DrinkCommand know that I want the drink (cappucino) with sugar and/or milk?

4
  • 1
    Why not pass the string "Capuccino + sugar" to the CommandParameter? Commented Nov 5, 2019 at 11:24
  • That's an option, however than I have to split the string in the DrinkCommand. I hoped there would be a more elegant option Commented Nov 5, 2019 at 11:26
  • Instead of setting <TextBlock>Capuccino + sugar</TextBlock> as the Button's Content, you could also just write Content="Capuccino + sugar", and move Command="{Binding DrinkCommand}" and CommandParameter="{Binding Content, RelativeSource={RelativeSource Self}}" into the Button Style. Commented Nov 5, 2019 at 11:27
  • You could use MultiBinding, SO answer. Or even an array Another SO Answer. Commented Nov 5, 2019 at 11:38

3 Answers 3

5

You could create a class to hold your different command parameters and use it like:

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

Your class would look like

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

and you could use it in your command code where it will be passed as argument.

Sign up to request clarification or add additional context in comments.

Comments

0

It exsists several way to reveal your need, following the use of combobox to select the type of command and a button to send the request:

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 >

Main Class

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;
            }
        }
}

Call Of View

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

Cordialy

Comments

0

you can use a single command with a single parameter which can be parsed in command method.

Such parameter can be of enum type, which declared witrh Flags attribute:

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

xaml:

<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}"/>

command method:

private void Drink(object args)
{
    string x = (string)args;
    if (x != null)
    {
        Tastes t = (Tastes)Enum.Parse(typeof(Tastes), x);
        if (t.HasFlag(Tastes.Sugar))
        {
            Console.WriteLine("sweet");
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.