Multiple parameters with Command binding
Asked Answered
Q

1

3

I have a textblock with command binding and using Prism library.

this is the XAML parth:

<TextBlock Margin="0,10,0,0">SocialSecurityNumer:</TextBlock>
<TextBox Name="SSNText" GotFocus="TextBox_GotFocus" Text="{Binding SSN, UpdateSourceTrigger=PropertyChanged}" Margin="0,3,0,0"/>

And this is the ViewModel behind:

public FindViewModel()
{
    var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();

    FindCommand = new DelegateCommand(
        () => eventAggregator.GetEvent<SSNChangedEvent>().Publish(SSN),
        () => !string.IsNullOrWhiteSpace(Kennitala)
        );
}

public DelegateCommand FindCommand { get; set; }

private string ssn;
public string SSN
{
    get { return ssn; }
    set
    {
        if (ssn== value)
            return;

        ssn = value;
        RaisePropertyChanged(() => SSN);
        FindCommand.RaiseCanExecuteChanged();
    }
}

And this is the GridViewModel that listen for this event trigger and fire up a function with SSN as a parameter

public class GridViewModel : NotificationObject
{
    public GridViewModel()
    {
        var eventAggregator = ServiceLocator.Current.GetInstance<IEventAggregator>();
        eventAggregator.GetEvent<SSNChangedEvent>().Subscribe(GetData);
    }

    public ObservableCollection<Investment> Investments { get; set; }

    private void GetData(string ssn)
    {
        var list = GeniusConnection.GetDataFromWebService(ssn);

        Investments = new ObservableCollection<Investment>(list);

        RaisePropertyChanged(() => Investment);
    }
}

How can i add another parameter, for example datetime parameter, the part that confuses me is:

FindCommand = new DelegateCommand(
            () => eventAggregator.GetEvent<SSNChangedEvent>().Publish(SSN),
            () => !string.IsNullOrWhiteSpace(Kennitala)
            );

This Publish function takes just one parameter and therefor i don´t see how i can easily add multiple paramters.??

Quan answered 2/9, 2011 at 14:30 Comment(3)
Please read the editing help and make sure to format your code properly the next time.Neume
is something wrong with the formatting ?Quan
Well, not anymore, but there was...Neume
S
6

you should create a class that holds all neccessary parameters that you want to publish.

 public class SSNChangedEventParams
 {
     public string SSN{get;set;}
     public DateTime Dt{get;set;}
     ...
 }

and then Publish an instance of this class:

 eventAggregator.GetEvent<SSNChangedEvent>().Publish(new SSNChangedEventParams(){SSN=SSN, Dt = DateTime.Now})
Smack answered 2/9, 2011 at 14:36 Comment(4)
@agh Your parameter can be whatever you want, so as suggested in this answer, make a container class that has the parameters you need and pass it in the constructor. A few times, I have gone as far as passing an entire ViewModel in a Command Parameter!Outsole
Hi, just quick question. The value that comes from the parameter DT is always Date = {1.1.0001 00:00:00} , but i pick another date in the datepickerQuan
@agh: that is the default value for a DateTime, please post another question if you have issues with that.Smack
Can that class be a generic class? I tried it when when I go to subscribe, it will not let me use a type parameter. (Defining a subscribe for every kind of type is not possible).Niobic

© 2022 - 2024 — McMap. All rights reserved.