I have a view in which there is a list of elements attached to my ViewModel (MVVM template).
Let's say it looks like this:
<ScrollViewer Width="Auto" Height="Auto"> <ItemsControl ItemsSource="{Binding Path=MessageLog}" Grid.IsSharedSizeScope="True" ScrollViewer.CanContentScroll="True"> <ItemsControl.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="150" SharedSizeGroup="FullName"/> <ColumnDefinition Width="*" SharedSizeGroup="MessageLog"/> </Grid.ColumnDefinitions> <StackPanel> <TextBlock Text="{Binding Path=PostedBy.FullName}" /> <TextBlock Text="{Binding Path=DatePosted}" /> </StackPanel> <TextBlock Grid.Column="1" Text="{Binding Path=MessageLog}"/> </Grid> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </ScrollViewer>
When the user adds something to the MessageLog (there is a MessageLog property in the VM), I want to automatically scroll to the last item.
In other words, I just want to automatically move the scroll bar when the user types a message and presses the enter button (something like Skype).
The binding to MessageLog works as expected, and the item is updated in the view. (I am pleased with this, and I want to leave it like that)
I am wondering if using the MVVM template template, can I still implement automatic scrolling in the code behind the View file? This looks logical, since the scroll behavior has nothing to do with the VM, and the ViewModel knows nothing about the view. It is right? Am I going right or is something missing?
Generally speaking, when adding an implementation to a view makes sense?
wpf mvvm binding
Novitzky
source share