Question

I am using MVVM tool kit version1. I have two Text boxes textbox1 and textbox2. I need to pass these two values as parameter when pressing on button and need to show the result on a third Text Box named textbox3.

my VM code similar like this

public ICommand AddCommand
    {
        get
        {
            if (addCommand == null)
            {
                addCommand = new DelegateCommand<object>(CommandExecute,CanCommandExecute);
            }
            return addCommand;
        }
    }

    private void  CommandExecute(object parameter)
    {
        var values = (object[])parameter;
        var a= (int)values[0];
        var b= (int)values[1];
        Calculater calcu = new Calcu();
        int c = calcu.sum(a, b);      
    }

    private bool  CanCommandExecute(object parameter)
    {
        return true;  
    }

The commandExecute method is called when the user click on the button but my the parameter argument doesn't not have any value. how i can pass the user's values as parameter?. and return the result to the texbox3?

Was it helpful?

Solution

you can use Multibinding and a Converter

<Button Content="Add" Command="{Binding AddCommand}"
 <Button.CommandParameter>
    <MultiBinding Converter="{StaticResource YourConverter}">
         <Binding Path="Text" ElementName="txt1"/>
         <Binding Path="Text" ElementName="txt2"/>
    </MultiBinding>
 </Button.CommandParameter>
</Button>

converter

public class YourConverter : IMultiValueConverter
{
 public object Convert(object[] values, ...)
 {
    //.Net4.0
    return new Tuple<int, int>((int)values[0], (int)values[1]);

    //.Net < 4.0
    //return values.ToArray();
 }

 ...
}

command

private void  CommandExecute(object parameter)
{
    var o= (Tuple<int, int>)parameter;
    var a= o.Item1;
    var b= o.Item2;
    Calculater calcu = new Calcu();
    int c = calcu.sum(a, b);      
}

ps: pls check my syntax - its written from my mind...

OTHER TIPS

You can make Properties in your ViewModel and bind it to your TextBoxs you need to use INotifyPropertyChanged to Notify the change....

View Model

public MyViewModel:INotifyPropertyChanged
{
private string1;
private string2;
private string3;

//Make a similar propety for string2 and string3
public string String1
{         
    get         
    {            
        return string1;         
    }
    set
    {
        string1=value;
        //Implementation of Propertychanged is left upon you
        InvokePropertyChanged("String1");
    }     
}    


public ICommand AddCommand     
{         
    get         
    {             
        if (addCommand == null)             
        {                 
            addCommand = new DelegateCommand<object>(CommandExecute,CanCommandExecute);             
        }             
        return addCommand;         
    }     
}      
private void  CommandExecute(object parameter)     
{           
    var a= String1;         
    var b= String2;         
    Calculater calcu = new Calcu();         
    String3 = (calcu.sum(a, b)).ToString();           
}      
private bool  CanCommandExecute(object parameter)     
{         
    return true;       
}
}

Xaml

<TextBox Text={Binding Path=String1}></TextBox>
<TextBoxText={Binding Path=String2}></TextBox>
<Button Command={Binding Path=AddCommand}>Add</Button>
<TextBoxText={Binding Path=String3></TextBox>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top