2

我是 Wp7 的新手,使用事件开发应用程序ListPicker并使用SelectionChanged事件来获取选定的数据表单 listPicker,但是SelectionChanged当我使用它并获取时遇到事件问题,NullReferenceException但是当我使用相同的代码时,button_Click它可以完美运行并获取选定的文本

我的 C# 代码是:

 private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ListPickerItem lpi = (ListPickerItem)listPicker1.SelectedItem;//this code is working in click event
        MessageBox.Show(lpi.Content.ToString());
    }

我的 Xaml 代码是

<toolkit:ListPicker x:Name="listPicker1" Grid.Row="0" ExpansionMode="ExpansionAllowed" SelectionChanged="listPickerCountryLogin_SelectionChanged" HorizontalAlignment="Left" Margin="14,43,0,0" VerticalAlignment="Top" Width="436" FullModeHeader="Select Country" Background="White" BorderBrush="White">
            <toolkit:ListPicker.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Country}" Width="250" />
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.ItemTemplate>
            <toolkit:ListPicker.FullModeItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Country}" Width="300" Margin="0,0,0,20" FontSize="44"/>
                    </StackPanel>
                </DataTemplate>
            </toolkit:ListPicker.FullModeItemTemplate>
        </toolkit:ListPicker>

但我想获取文本表单SelectionChanged事件?

如何实现这一点:)

提前致谢

4

4 回答 4

4

我也遇到了同样的问题我也遇到了NullReferenceException

试试这个对我来说很好

1)如果您使用静态ListPickerItems方式而不DataBinding使用此

private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ListPickerItem lpi = (sender as ListPicker).SelectedItem as ListPickerItem;
    MessageBox.Show("selected item is : " + lpi.Content);
}

2) 如果您正在使用DataBinding显示listPickerItems

 private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     Countries item = (sender as ListPicker).SelectedItem as Countries;
     MessageBox.Show("Selected Item is : " + item.Country);
}

这里假设您准备了一个Countries具有country属性的类,您需要将所选项目类型转换为国家类然后只有您得到结果

于 2013-02-08T07:08:29.690 回答
1

在我的列表选择器中使用 SelectionChanged 事件时,我遇到了同样的问题,这就是我所拥有的:

// My listpicker is LpBluetoothPaired

 private void LpBluetoothPaired_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

         rcvData.Text = LpBluetoothPaired.SelectedItem.ToString();

    }

但是打开应用程序时出现异常,所以我修复了它:

 private void LpBluetoothPaired_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

       if (LpBluetoothPaired.SelectedItem != null) {
            rcvData.Text = LpBluetoothPaired.SelectedItem.ToString();
        }
    }

看起来在应用程序打开时调用了该事件,但当时仍然没有 selectedItem,因此为了避免异常并仅满足 rcvData 文本框,我检查它是否不为空

于 2014-01-03T00:47:03.523 回答
0

从后面的代码设置 ItemsSource 时,有时会触发 SelectionChanged 事件。因此,在这种情况下 selectedItem 可能为空。

因此,在您的 SelectionChanged 代码中添加这一行并尝试。

private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if(listPicker1.SelectedIndex == -1) //otherwise (listPicker1.SelectedItem == null) also works
    return;
    ListPickerItem lpi = (ListPickerItem)listPicker1.SelectedItem;//this code is working in click event
    MessageBox.Show(lpi.Content.ToString());
}

如果问题仍然存在,请在 SelectionChanged 处理程序中放置一个断点,并观察值

于 2013-01-29T08:14:23.987 回答
0

当数据加载到 listpicker 时,它会触发 selectionchanged 事件。因此,为此将您的代码放入 Try-catch 中,对于 selectionchanged,请写下:

private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
     try
     {
        Countries SelectedCountries =e.AddedItems[0] as Countries;
        String SelectedCountry = SelectedCountries.Country;
     }
     catch
     {
     }
}
于 2013-05-20T09:15:35.820 回答