Ivan
Ivan

Reputation: 1171

How to call ListView SelectionChanged method?

I have the following method that is called when ListView item selection is changed:

private void SlideTransitionsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

.........
}

I would like to call this method from some other method. How would I do that?

Upvotes: 1

Views: 1170

Answers (3)

ΩmegaMan
ΩmegaMan

Reputation: 31721

If the paramters of sender and e are not used, simply call the method directly from any other method.

SlideTransitionsList_SelectionChanged(null, null);

Note that you are not firing off an event by calling this method; this is just the event's callback method and can be used by any other method.


The advice given in the other answers are just as correct, but are simply syntatic suger for coding paradigms and can be used as preference or styles are dictated.

Upvotes: 0

redwards510
redwards510

Reputation: 1922

That is an event handler. It's fired on UI actions. Calling it directly isn't a great idea. Put a method inside it, and then you can call that method if you need to from other places.

private void SlideTransitionsList_SelectionChanged(object sender,     SelectionChangedEventArgs e)
{
    DoSomeStuffOnSelectionChanged();
}

public void DoSomeStuffOnSelectionChanged()
{
    // enter code here
}

Upvotes: 5

Backs
Backs

Reputation: 24913

Extract all code from SlideTransitionsList_SelectionChanged handler to other method and call it.

private void SlideTransitionsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DoSmth();
}
...
private void OtherMethod()
{
    DoSmth();
}

Upvotes: 2

Related Questions