私の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」という名前のテキストボックスが1つあります。私はその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ページの次のコントロールです)。誰かがそれに何が悪いのか提案できますか?