我有一个视图,它具有绑定到我的ViewModel(MVVM模式)的项目列表。MVVM。在某些情况下向View添加代码是否合理?
比方说,它看起来像这样:
<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>
当用户添加的东西MessageLog(存在虚拟机属性MessageLog)我要自动滚动到最近的项目。
换句话说,我只是想在用户输入消息并点击输入时自动移动滚动条(就像Skype一样)。
在MessageLog上的绑定按预期工作,并在视图上更新项目。 (我很高兴这一点,我想离开它那样)
我想知道如果使用MVVM模式的方法,我仍然可以在视图的文件后面的代码中实现自动滚动?这似乎是相当逻辑的,因为滚动行为与虚拟机没有任何关系,ViewModel也不知道任何有关View的内容。这样对吗?我正确的方式或者我错过了什么?
一般来说,向View添加实现有意义吗?
任何附加的属性/行为的例子,我想更多地了解这个! – 2010-10-25 16:34:32
@pete:我写了一篇描述使用行为的文章:http://reedcopsey.com/2009/10/09/using-behaviors-to-allow-the-viewmodel-to-manage-view-lifetime-in-mv -vm/ – 2010-10-25 16:37:04
@pete:Nishant Sivakumar也将它移植到标准附属支柱上。请参阅:http://reedcopsey.com/2010/04/15/attached-property-port-of-my-window-close-behavior/ – 2010-10-25 16:37:33