listbox.itemssource = null;在wp7中抛出异常

时间:2012-06-06 10:17:26

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

我有一个列表框和一个文本框。我想处理它的keyup事件,但它给了我一个错误。

<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的替代品。请告诉我任何解决方法。

2 个答案:

答案 0 :(得分:0)

那是因为它会触发lstSelectedNumber_SelectionChanged事件。 将调试器放在异常语句上并按F11将转到此事件。

替换

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

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

答案 1 :(得分: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;
                });
            }
        }