0

リストボックスとテキストボックスがあります。キーアップイベントを処理したいのですが、エラーが発生します。

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



private void txtNumber_KeyUp(object sender, KeyEventArgs e)
      {
          TextBox txtbox = sender as TextBox;
          if (txtbox.Text.Contains(';'))
          { 
              lstSelectedNumber.ItemsSource = null;
              // My Application Got crashed at this point when i assign nullto item source
              lstSelectedNumber.ItemsSource = lstContactModel;
          }

私の更新されたコレクションがそのリストボックスのitemsourceであるという代替案はありますか?そのための回避策を教えてください。

4

2 に答える 2

0

それはlstSelectedNumber_SelectionChangedイベントを発生させるからです。デバッガーを例外ステートメントに置き、F11キーを押すと、このイベントが発生します。

交換

lstSelectedNumber.ItemsSource = null;
lstSelectedNumber.ItemsSource = lstContactModel;

lstSelectedNumber.SelectionChanged -= lstSelectedNumber_SelectionChanged;
lstSelectedNumber.ItemsSource = null;
lstSelectedNumber.ItemsSource = lstContactModel;
lstSelectedNumber.SelectionChanged += lstSelectedNumber_SelectionChanged;
于 2012-06-06T10:22:33.573 に答える
0

私はこの問題を自分で修正しました。問題は、テキストボックスイベントが呼び出されると、リストにいくつかの変更が加えられ、空のソースがリストボックスにバインドされることです。この変更は、UIとUIがその変更を処理できないため、すべてのコードをディスパッチャに配置します。物事は完了しましたそれはUIへの変更を反映し、UIはそれを受け入れます

 private void txtNumber_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox txtbox = sender as TextBox;
            if (txtbox.Text.Contains(';'))
            {
                Dispatcher.BeginInvoke(() =>
                { 
                    lstSelectedNumber.ItemsSource = null;
                    lstSelectedNumber.ItemsSource = lstContactModel;
                });
            }
        }
于 2012-06-07T06:48:01.460 に答える