简体   繁体   中英

listbox scroll to end in wp7

I have listbox in my wp7 app like this

   <ListBox Name="lstSelectedNumber" Height="50" MaxHeight="120" VerticalAlignment="Top" Grid.Column="1" SelectionChanged="lstSelectedNumber_SelectionChanged">
                            <ListBox.ItemContainerStyle>
                                <Style TargetType="ListBoxItem">
                                    <Setter Property="Padding" Value="-15" />
                                    <Setter Property="Margin" Value="0"/>
                                </Style>
                            </ListBox.ItemContainerStyle>
                            <ListBox.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <toolkit:WrapPanel>
                                    </toolkit:WrapPanel>
                                </ItemsPanelTemplate>
                            </ListBox.ItemsPanel>
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel>
                                    <TextBox x:Name="txtNumber" Text="{Binding Name,Mode=TwoWay}" IsEnabled="{Binding IsEnabled,Mode=TwoWay}" Background="Transparent" Foreground="{StaticResource ContactSelectorBrush}" Style="{StaticResource DialNumberStyle}" FontSize="24" KeyUp="txtNumber_KeyUp">
                                        <TextBox.CaretBrush>
                                            <SolidColorBrush Color="{StaticResource CaretBrush}" />
                                        </TextBox.CaretBrush>
                                    </TextBox>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>

And in data template of my listbox there is one Textbox Named "txtNumber". I am calling its Textchange Event and on its textchange I'm doing some operations like this

TextBox txtbox = sender as TextBox;
                Dispatcher.BeginInvoke(() =>
                {
                    ContactModel model = lstContactModel.LastOrDefault();
                    if (string.IsNullOrEmpty(model.Name) && string.IsNullOrEmpty(model.Phone))
                    {
                        lstContactModel.Remove(model);
                        lstContactModel.Add(new ContactModel { Name = txtbox.Text, Phone = txtbox.Text + ",", IsEnabled = false });
                    }

                    lstSelectedNumber.ItemsSource = null;
                    lstSelectedNumber.ItemsSource = lstContactModel;
                    var Selecteditem = lstSelectedNumber.Items[lstSelectedNumber.Items.Count - 1];
                    lstSelectedNumber.ScrollIntoView(Selecteditem);
                    lstSelectedNumber.UpdateLayout();
                });

Im adding new item to my list and then rebinding to my listbox and I'm scrolling to end of my listbox, but it's not working.

It shows very odd behavior. Once This Statement Runs It adds the item and focus goes to another textbox which is not in this listbox (it's the next control in my wp7 page). Can anyone suggest what is wrong in it?

Is there a reason, why you removed the ItemsSource and setting it again. I would suggest using an ObservableCollection and let the DataBinding engine do its magic.

lstbox.Dispatcher.BeginInvoke(() =>
                {
                    lstbox.ItemsSource = null;
                    lstbox.ItemsSource = lstContactModel;
                    var Selecteditem = lstbox.Items[lstbox.Items.Count - 1];
                    lstbox.ScrollIntoView(Selecteditem);
                    lstbox.UpdateLayout();
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM