Creating an instance of SelectionChangedEventArgs in wpf
-
Hi Experts, I have a user control which is having a listview inside it. The SelectionChanged event of this list view is handled inside the user control. The code for the same is as follows:
private void lstvMyView\_SelectionChanged(object sender, SelectionChangedEventArgs e) {...}
I want to call this handler again from some other place inside the user control. So to call this handler i need the "SelectionChangedEventArgs" there. When I am trying to create the instance of "SelectionChangedEventArgs", i am unable to understand what should I pass as parameters to the constructor of "SelectionChangedEventArgs". The place from where I am suppose to call this handler does not add or remove any items in the listview. It just navigates in the items in the listview thereby changing the selectedindex of the listview. I am trying to do something like this. The below code is obviously incorrect.
lstvMyView_SelectionChanged(_lstvMyView, new SelectionChangedEventArgs());
Please Help! Thanks in Advance! Regards, Samar
modified on Wednesday, September 29, 2010 4:42 AM
-
Hi Experts, I have a user control which is having a listview inside it. The SelectionChanged event of this list view is handled inside the user control. The code for the same is as follows:
private void lstvMyView\_SelectionChanged(object sender, SelectionChangedEventArgs e) {...}
I want to call this handler again from some other place inside the user control. So to call this handler i need the "SelectionChangedEventArgs" there. When I am trying to create the instance of "SelectionChangedEventArgs", i am unable to understand what should I pass as parameters to the constructor of "SelectionChangedEventArgs". The place from where I am suppose to call this handler does not add or remove any items in the listview. It just navigates in the items in the listview thereby changing the selectedindex of the listview. I am trying to do something like this. The below code is obviously incorrect.
lstvMyView_SelectionChanged(_lstvMyView, new SelectionChangedEventArgs());
Please Help! Thanks in Advance! Regards, Samar
modified on Wednesday, September 29, 2010 4:42 AM
Just because there's a
SelectionChangedEventArgs
parameter there, it doesn't mean you have to pass anything to it. If you don't actually do anything in the method with the valuee
, you can simply passnull
in as the parameter.lstMyView_SelectionChanged(lstMyView, null);
Similarly, if you don't do anything with sender, you can always pass null in there. A simpler technique would be to move the functionality that
lstMyView_SelectionChanged
implements out into a separate method and call that directly (and from the event handler as well). Now that we've covered that, I would urge you to look into MVVM to stop relying on the code behind here. If your method is updating a model in some way, you really should separate it out of the view specific code (obviously, if you are just triggering an animation or other view option then what you are doing here is perfectly fine and shouldn't be refactored outside the view).I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
Just because there's a
SelectionChangedEventArgs
parameter there, it doesn't mean you have to pass anything to it. If you don't actually do anything in the method with the valuee
, you can simply passnull
in as the parameter.lstMyView_SelectionChanged(lstMyView, null);
Similarly, if you don't do anything with sender, you can always pass null in there. A simpler technique would be to move the functionality that
lstMyView_SelectionChanged
implements out into a separate method and call that directly (and from the event handler as well). Now that we've covered that, I would urge you to look into MVVM to stop relying on the code behind here. If your method is updating a model in some way, you really should separate it out of the view specific code (obviously, if you are just triggering an animation or other view option then what you are doing here is perfectly fine and shouldn't be refactored outside the view).I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
hi Pete, I cannot pass null to the parameter because i am doing something with e in inside the handler. Sorry to have not mentioned it here. Regards, Samar
-
hi Pete, I cannot pass null to the parameter because i am doing something with e in inside the handler. Sorry to have not mentioned it here. Regards, Samar
Really, you shouldn't be calling the event handler directly - you should use a method that accepts the arguments you are interested in and call that from your event handler. Here[^] is more information on what the constructor takes; as a hint, a question like this could be easily answered for you by looking up the documentation in MSDN.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
Really, you shouldn't be calling the event handler directly - you should use a method that accepts the arguments you are interested in and call that from your event handler. Here[^] is more information on what the constructor takes; as a hint, a question like this could be easily answered for you by looking up the documentation in MSDN.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
Hi Pete, The link you have provided gives just a general information on "SelectionChangedEventArgs" class. Also even if I create a method as you have told still i would need to initialize the class somewhere in the said method, right? Or maybe i am not able to understand what you are trying to say. Can you please give me a small code snippet of the functionality you mentioned? Thanks for your valuable time here. Regards, Samar
-
Hi Pete, The link you have provided gives just a general information on "SelectionChangedEventArgs" class. Also even if I create a method as you have told still i would need to initialize the class somewhere in the said method, right? Or maybe i am not able to understand what you are trying to say. Can you please give me a small code snippet of the functionality you mentioned? Thanks for your valuable time here. Regards, Samar
You don't need to initialize the class in the method - all you need to do is have a method that accepts the relevant IList(s) that you are interested in, and work with it there. For instance:
private void MyMethod(IList addedItems, IList removedItems)
{
// Do something here.
}Then all you need do in your event handler is call
MyMethod(addedItems, removedItems);
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
-
You don't need to initialize the class in the method - all you need to do is have a method that accepts the relevant IList(s) that you are interested in, and work with it there. For instance:
private void MyMethod(IList addedItems, IList removedItems)
{
// Do something here.
}Then all you need do in your event handler is call
MyMethod(addedItems, removedItems);
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads
Hi Pete thanks for your answer. This looks good. Just 1 question. Say I have a listview with 5 items in it and that the 2nd item is selected then "addedItems" is the 2nd item and "removedItems" are the rest of the 4 items. Am I right on this part? Regards, Samar
-
Hi Pete thanks for your answer. This looks good. Just 1 question. Say I have a listview with 5 items in it and that the 2nd item is selected then "addedItems" is the 2nd item and "removedItems" are the rest of the 4 items. Am I right on this part? Regards, Samar
Not quite. AddedItems refers to the items that were added to the selection since the last time the event was fired, and RemovedItems refers to the items that were removed from the selection since the last time the event was fired. This means that the changes effectively represent deltas and are cumulative.
I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be
Forgive your enemies - it messes with their heads