简体   繁体   中英

listbox.itemsource = null; throwing exception in wp7

I have a listbox and a textbox. I want to handle its keyup event but its giving me an error.

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

Is there any alternate that my updated collection is itemsource of that listbox. please tell me any work around for that.

That's because it fires lstSelectedNumber_SelectionChanged event. Putting a debugger on exception statement and pressing F11 will take you to this event.

Replace

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

with

lstSelectedNumber.SelectionChanged -= lstSelectedNumber_SelectionChanged;
lstSelectedNumber.ItemsSource = null;
lstSelectedNumber.ItemsSource = lstContactModel;
lstSelectedNumber.SelectionChanged += lstSelectedNumber_SelectionChanged;

I Have Fixed This Issue Myself. The Problem is This When My textbox Event is Called It Do Some Changes in My List And Bind Empty Source To My Listbox And This Change Effects My UI And UI Is Unable To Handle That Change So I Put My All Code In Dispatcher So Once All The Things Are Done It Reflects Changes To UI and UI Accepts That

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

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