是Wp7的新用户,使用ListPicker
开发应用并使用SelectionChanged
事件从listPicker中获取所选数据,但在使用此问题并获取SelectionChanged
事件时遇到问题{1}}但是当我在NullReferenceException
中使用相同的代码时,它完全正常并且正在获取所选文本
我的c#代码是:
button_Click
我的Xaml代码是
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());
}
但我希望获得<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>
事件的文字形式?
如何实现这一点:)
提前致谢
答案 0 :(得分: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
,因为您需要键入“选择项目到国家/地区”类别,然后才能获得结果
答案 1 :(得分:1)
我在listpicker中使用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 textBox,我检查它是否为null
答案 2 :(得分:0)
当从代码后面设置ItemsSource时,有时会触发SelectionChanged事件。因此,在这种情况下,selectedItem可能为null。
因此,在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处理程序中放置一个断点,并观察值
答案 3 :(得分:0)
当数据加载到listpicker时,它会触发selectionchanged event.So为此将代码放入Try-catch和For selectionchanged中写下:
private void listPicker1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
Countries SelectedCountries =e.AddedItems[0] as Countries;
String SelectedCountry = SelectedCountries.Country;
}
catch
{
}
}