【问题标题】:listbox scroll to end in wp7列表框滚动到 wp7 结束
【发布时间】:2012-06-07 12:49:29
【问题描述】:

我的 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 页面中的下一个控件)。谁能建议其中有什么问题?

【问题讨论】:

    标签: c# silverlight windows-phone-7 xaml silverlight-toolkit


    【解决方案1】:

    是否有原因,为什么您删除了ItemsSource 并重新设置它。 我建议使用ObservableCollection 并让DataBinding 引擎发挥它的魔力。

    【讨论】:

    • 我没有使用 wolfgang ziegler 任何可观察的集合。我正在使用简单的通用集合列表。
    【解决方案2】:
    lstbox.Dispatcher.BeginInvoke(() =>
                    {
                        lstbox.ItemsSource = null;
                        lstbox.ItemsSource = lstContactModel;
                        var Selecteditem = lstbox.Items[lstbox.Items.Count - 1];
                        lstbox.ScrollIntoView(Selecteditem);
                        lstbox.UpdateLayout();
                    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多