簡體   English   中英

列表框滾動到以wp7結尾

[英]listbox scroll to end in wp7

我的wp7應用中有這樣的列表框

   <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>

在我的列表框的數據模板中,有一個名為“ txtNumber”的文本框。 我正在調用它的Textchange事件,在它的textchange我正在執行類似的操作

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();
                });

我將新項目添加到列表中,然后重新綁定到列表框,並且滾動到列表框的末尾,但無法正常工作。

它顯示出非常奇怪的行為。 一旦運行此語句,它將添加項目,並且焦點將轉到不在此列表框中的另一個文本框(這是我的wp7頁面中的下一個控件)。 任何人都可以提出錯誤之處嗎?

是否有原因,為什么您刪除了ItemsSource並再次進行設置。 我建議使用ObservableCollection並讓DataBinding引擎發揮作用。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM