2

I'm currently working on a data driven editing tool that was written in WPF using MVVM. The primary display is a scrollable list of view models, some (not all) of which have inner lists of their own child view models (not scrollable). The problem is that one of the view model types is an array type that includes functionality to add a new child item and we want to make it so that if you use that, it then scrolls the overall list to that new item. Is there a reasonable way to do this using MVVM?

To give you an idea of how this UI is currently set up, this is the overall display:

<Grid ScrollViewer.VerticalScrollBarVisibility="Auto">
  <Label Content="{Binding Path=DisplayName}" Height="28" HorizontalAlignment="Left" Margin="4,4,0,0" VerticalAlignment="Top" />
  <ItemsControl IsTabStop="False" ItemsSource="{Binding Path=VMEntries}" Margin="12,25,12,12" ItemTemplateSelector="{StaticResource EntryTemplateSelector}" ScrollViewer.VerticalScrollBarVisibility="Auto">
    <ItemsControl.Template>
      <ControlTemplate>
        <ScrollViewer x:Name="ScrollViewer">
          <ItemsPresenter />
        </ScrollViewer>
      </ControlTemplate>
    </ItemsControl.Template>
  </ItemsControl>
</Grid>

and this is the data template for the array entry that we're working with:

<DataTemplate x:Key="Array">
  <Grid Margin="2,7,0,0">
    <Label Content="{Binding Path=DisplayName}" ToolTip="{Binding Path=Tooltip}"/>
    <Button Content="Add" HorizontalAlignment="Right" VerticalAlignment="Top"  Height="24" Width="24" Command="{Binding Path=AddCommand}"/>
    <ItemsControl HorizontalAlignment="Stretch" IsTabStop="False" ItemsSource="{Binding Path=SubEntries, NotifyOnSourceUpdated=True}" Margin="10,24,0,0" >
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="Auto" />
              <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button Name="RemoveButton" Grid.Column="0" Margin="0,5,0,0" Content="Del" HorizontalAlignment="Right" VerticalAlignment="Top"  Height="24" Width="24" Command="{Binding Path=RemoveCommand}" CommandParameter="{Binding Path=.}"/>
            <ContentControl Grid.Column="1" Content="{Binding Path=.}"  ContentTemplateSelector="{StaticResource EntryTemplateSelector}" />
          </Grid>
          <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=RemoveHandler}" Value="{x:Null}">
              <Setter Property="Visibility" TargetName="RemoveButton" Value="Collapsed"/>
            </DataTrigger>
          </DataTemplate.Triggers>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
  </Grid>
</DataTemplate>

1 Answer 1

0

The MVVM ideal is not adding code behind, but for some complicated things the easier way is to add it. For several cases if you want add complicated behaviors on your application and keeping the MVVM, an alternative is use the Behaviors (c# codes that allows be used and binds from XAML). Also you could define behaviors using AttachedProperties and register to the PropertyChanged event. And another alternative is to create a UserControl and add the code behind to it.

In your particular case, the inner collection must raise some event when add items to it, and then in your outer collection execute something like this list.ScrollIntoView(itemToScroll);. Hope this could give some tips to go on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.