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

3
  • Please read the editing help and make sure to format your code properly the next time. Commented Sep 2, 2011 at 14:32
  • is something wrong with the formatting ? Commented Sep 2, 2011 at 16:06
  • Well, not anymore, but there was... Commented Sep 2, 2011 at 16:16

1 Answer 1

7

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})
Sign up to request clarification or add additional context in comments.

4 Comments

@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!
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
@agh: that is the default value for a DateTime, please post another question if you have issues with that.
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).

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.