views:

45

answers:

1

I am studying to use MVVM pattern for my Silverlight application.

Following code is from xaml UI code :

<Button Width="30" 
        Margin="10" 
        Content="Find"
        Command="{Binding Path=GetCustomersCommand, Source={StaticResource customerVM}}"
        CommandParameter="{Binding Path=Text, ElementName=tbName}"/>

<TextBox x:Name="tbName" 
         Width="50" />

<TextBox x:Name="tbID" 
         Width="50" />

And following code is from ViewModel class :

public ICommand GetCustomersCommand
{
    get { return new RelayCommand(GetCustomers) { IsEnabled = true }; }
}

public void GetCustomers(string name, string id)
{
    // call server service (WCF service)
}

I need to pass two parameters, however, can't find out how to pass two parameters(id and name) to ViewModel class.

I'd like to know if it is possible in xaml code not in the codebehind.

Thanks in advance

+1  A: 

There's no easy way to do it. Instead, I suggest you make a command with no parameters, and bind box TextBoxes to properties of your ViewModel:

C#

public void GetCustomers()
{
    GetCustomers(_id, _name);
}

private int _id;
public int ID
{
    get { return _id; }
    set
    {
        _id = value;
        OnPropertyChanged("ID");
    }
}

private string _name;
public string Name
{
    get { return _name; }
    set
    {
        _name = value;
        OnPropertyChanged("Name");
    }
}

XAML

<Button Width="30" 
        Margin="10" 
        Content="Find"
        Command="{Binding Path=GetCustomersCommand, Source={StaticResource customerVM}}"/>

<TextBox x:Name="tbName"
         Text="{Binding Path=Name, Source={StaticResource customerVM}, Mode=TwoWay}"
         Width="50" />

<TextBox x:Name="tbID" 
         Text="{Binding Path=ID, Source={StaticResource customerVM}, Mode=TwoWay}"
         Width="50" />
Thomas Levesque
Thank you for your reply, it solves my problem, however it seems multi binding featere is needed in Silverlight, though.
kwon
Multibinding ? why ? You're only binding to one property at a time, so a simple binding works fine
Thomas Levesque