3
votes

I am using WPF MVVM with C#. I have a Scrollviewer in a UserControl and I need the following functionality that I haven't been able to work out how to do which is basically:

When an Item gets added to the content of my ScrollViewer; if the item added is not visible I would like my ScrollViewer to scroll down so that I can view my newly added Item in my ListView. I have been able to bind the selected item successfully but not sure how to make it scroll to it.

That's all there really is to it but I'm not sure how to do this. If there's any comments or questions I'll try to suitably amend the post, I've included the .xaml below

Thanks

     <ScrollViewer  Background="Pink" HorizontalAlignment="Left" Height="173" x:Name="ScrollViewer1" Width="560" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden">

                                <Grid Name="GridValuesAndpartss" VerticalAlignment="Top"  Height="165">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="370" />
                                        <ColumnDefinition Width="204" />

                                    </Grid.ColumnDefinitions>
                                    <ListView  SelectedItem="{Binding SelectedBetmyValue, Mode=TwoWay}" ItemsSource="{Binding Values}"  Name="BetValuesListView" Height="Auto"  Margin="0,0,0,0"  myValueMode="Single" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                       <ListView.View>
                                            <GridView>
                                                <GridViewColumn  Header="Price      "    Width="95">
                                                    <GridViewColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal"  Margin="-7,0,0,0" MinWidth="95" Width="Auto">                                                                   
                                                                <TextBlock Text="{Binding Path=PriceTypeCode}" Foreground="Black" FontSize="10" ToolTip="Price Type Code" />
                                                                <TextBlock Text=":" Foreground="Black" FontSize="10" ToolTip="Price Type Code" />
                                                                <TextBlock Text="{Binding Path=PriceTaken,Converter={StaticResource myValuePriceDisplayConverter}}" Foreground="Red" FontSize="10" ToolTip="Price Taken"  />
                                                                <TextBlock Text="." FontSize="4" />
                                                                <TextBlock Text="{Binding Path=PriceCurrent,Converter={StaticResource myValuePriceDisplayConverter}}" Foreground="Black" FontSize="10" ToolTip="Price @ Scan Time"  />
                                                                <TextBlock Text="." FontSize="4" />                                                                       
                                                                <TextBlock Text="{Binding Path=PriceSP,Converter={StaticResource myValuePriceDisplayConverter}}" Foreground="Green" FontSize="10" ToolTip="Price SP"   />
                                                            </StackPanel>



                                                        </DataTemplate>
                                                    </GridViewColumn.CellTemplate>
                                                </GridViewColumn>
                                            </GridView>
                                        </ListView.View>
                                    </ListView>

                                </Grid>

                            </ScrollViewer>
1
Where are you adding new items? ListView?123 456 789 0
Did you try BetValuesListView.ScrollIntoView() after the code to add the new item to Listview ?VSS

1 Answers

0
votes

I would try to use here the code behind a little bit.

  1. Give the ScrollViewer a name (like 'x:Name="MyScrolly"').
  2. Listen to the 'SelectionChanged'-Event of ListView.
  3. In the event handler of selection changed event (code behind) call:

    MyScrolly.ScrollToBottom();

I think the new item is always at the bottom. If not try this method: 'ScrollToVerticalOffset()'.