Skip to content
This repository has been archived by the owner on May 7, 2022. It is now read-only.

SelectionChanged event passes EventArgs instead of SelectionChangedEventArgs #39

Open
AnotherKiwi opened this issue Apr 1, 2017 · 4 comments

Comments

@AnotherKiwi
Copy link

First, many thanks for this excellent control!

I'm having trouble, though, gaining access in my viewmodel to the SelectedItems property. I haven't been able to get binding to work (probably my fault, as I am new to WPF and MVVM), so I thought I would hook the SelectionChanged event and access the SelectionChangedEventArgs.AddedItems and SelectionChangedEventArgs.RemovedItems properties. Unfortunately when the SelectionChanged event is raised it seems to pass a null EventArgs object, rather than a SelectionChangedEventArgs object.

Could this be fixed? If possible, it would be really helpful if the SelectedItems were included as a property of SelectionChangedEventArgs as well as the AddedItems and RemovedItems.

@ygoe
Copy link
Owner

ygoe commented Apr 2, 2017

I'll look into it. But in the meantime, what keeps you from accessing the current SelectedItems property in your event handler? I'm not sure whether it can be easily determined how the selected items collection has changed, but at least we know that it has changed and you can request the current collection then.

@AnotherKiwi
Copy link
Author

I'm moving all event handlers out of the code behind into my view model (to fit with MVVM), so need the SelectedItems to be passed in the event args.

I explained myself poorly above. In my view model I've successfully hooked the PreviewSelectionChanged event, and can access the AddedItems and RemovedItems properties of the PreviewSelectionChangedEventArgs. But, after the selected items have changed, I hoped to be able to access the SelectedItems using the SelectionChanged event. Unfortunately that doesn't pass anything in its event args.

I've found that I can achieve this by adding a SelectedItemsChangedEventArgs class that has a SelectedItems property, and by changing the SelectionChanged event declaration in MultiSelectTreeView.cs to:
public event EventHandler<SelectedItemsChangedEventArgs> SelectionChanged;

Hopefully you will consider doing this, or something similar, to make the SelectedItems available from the SelectionChanged event.

@ygoe
Copy link
Owner

ygoe commented Apr 2, 2017

Ehm, to my knowledge, according to the MVVM pattern, event handlers belong in the code behind and not in the view model. View and view model are practically only connected by data binding. How did you manage to have event handlers in the view model class at all? XAML won't look for them there on its own…

@AnotherKiwi
Copy link
Author

I'm using Catel's EventToCommand trigger which turns an event to a command in the view model that can be connected using data binding.
https://catelproject.atlassian.net/wiki/display/CTL/EventToCommand

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants