I am developing a WPF / MVVM application and I have a list binding to data in a ViewModel. At different points, I need a view model to cause the listbox to scroll to this item.
How can I do this without creating a custom control and maintaining a good separation of concerns?
I am currently working on creating my own class of behavior at the presentation level with a dependency property VisibleIndex, which then encodes the XAML code to an integer in the presentation model:
<ListBox x:Name="myListBox"
local:ListBoxVisibilityBehavior.VisibleIndex="{Binding VisibleIndex}">
When an integer is given, it starts the dependency property update handler, which tells the list to jump to the corresponding index.
This seems a bit hacked, although due to the fact that the value of the dependency property never changes in the list, and the update handler is called only when the value changes, so the only way to make sure that the visible element is visible is to do something like this
this.VisibleIndex = -1;
this.VisibleIndex = 10;
The only reason I'm using the behavior class right now is to bind my custom dependency property, is there a way to do something similar with events?
source
share