Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi
I have been experimenting with WPF and MVVM recently after being solely WinForms for a long time. Bit late to the game I know!

I have a window that has 2 combo boxes that are used for filtering a ListBox. I'd like to be able to have the user make a selection in the ListBox then when they then remove the filters, their selection remains and the list view automatically scrolls to it.

This can be done by calling the ScrollIntoView method of the ListBox and I've achieved it by using an event in the code behind the window.

Is there a way to do this in a more MVVM way or is the event way the best way to do it?

What I have tried:

private void cBox_Selected(object sender, RoutedEventArgs e)
{
    errorListBox.ScrollIntoView(errorListBox.SelectedItem);
}


<ComboBox DockPanel.Dock="Left"
          Width="100"
          ItemsSource="{Binding AvailableFilters, UpdateSourceTrigger=PropertyChanged}"
          SelectedItem="{Binding SelectedFilter}"
          Name="cBox"
          SelectionChanged="cBox_Selected"
          >
Posted
Updated 19-Jul-18 3:45am
v2
Comments
Nathan Minier 18-Jul-18 11:51am    
Scrolling to a specific item is strictly a part of the presentation (it has no bearing whatsoever on business logic), so an event handler on the view is a perfectly legitimate approach. Ignore those weird purists that claim that the code behind must be effectively empty; the tool is there to be used, and by mixing strict presentation logic into VMs you spoil the point of the entire architecture. For your own sanity I cannot advocate that approach enough.
TheFoZ 19-Jul-18 3:09am    
Hi Nathan

Thanks for your response. I like the idea of trying to keep my sanity!
Nathan Minier 19-Jul-18 7:13am    
I try to help. Playing MVVM games almost cost me mine :)

1 solution

Nathan has given me a great answer in the comments
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900