views:

71

answers:

1

I have a View which has a list of items bound to my ViewModel (MVVM pattern).

Let's say it looks like that:

<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 user adds something to MessageLog (there is a property MessageLog in VM) I want to automatically scroll to the most recent item.

In other words, I just want to move the scrollbar automatically when user type a message and hit the enter (something like Skype does).

Binding on MessageLog works as expected and item is updated on the view. (I'm happy about that and I want to leave it like that)

I am wondering if using MVVM pattern approach, can I still implement an auto scroll in code behind file of the View? It seems to be quite logic as scrolling behavior has nothing to do with the VM and ViewModel doesn't know anything about the View. Is it right? Am I going right way or I am missing something?

Generally speaking, when adding an implementation to a View makes sense?

+8  A: 

Yes, this is perfectly acceptable. Since the logic here is 100% View related, there is no problem with adding it to the View.

MVVM is about separating your Application logic from your View logic, not necessarily about stripping 100% of the code out of your View.

That being said, there are alternatives to code behind for this. Attached properties (or Behaviors) are a great option for tasks like these - they have the large advantage of being reusable in other Views later, so you don't reinvent this later if you decide you want the same behavior in other parts of your User Interface.

Reed Copsey
Any examples of the attached properties / behaviours, I'd be interested to learn more about this!
pete the pagan-gerbil
@pete: I wrote an article describing using a Behavior: http://reedcopsey.com/2009/10/09/using-behaviors-to-allow-the-viewmodel-to-manage-view-lifetime-in-m-v-vm/
Reed Copsey
@pete: Nishant Sivakumar ported it to a standard attached prop, too. See: http://reedcopsey.com/2010/04/15/attached-property-port-of-my-window-close-behavior/
Reed Copsey
@Reed Copsey: Thanks for answering this question. As I mentioned before it is exactly what I was thinking. I like your explanation that the whole MVVM is not about stripping 100% code out of the View. Now I am going to find out more about Attached properties and Behaviors. Thanks again.
Novitzky
Excellent, thank you very much!
pete the pagan-gerbil