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

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
aghaux
  • 729
  • 4
  • 14
  • 38

1 Answers1

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})
thumbmunkeys
  • 20,606
  • 8
  • 62
  • 110
  • 1
    @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! – EtherDragon Sep 02 '11 at 23:00
  • 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 datepicker – aghaux Sep 05 '11 at 10:18
  • @agh: that is the default value for a DateTime, please post another question if you have issues with that. – thumbmunkeys Sep 05 '11 at 11:49
  • 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). – Vaccano Aug 03 '12 at 20:04