如何在c#for windows phone应用程序中以字符串形式从列表选择器中获取selecteditem
本文关键字:列表 selecteditem 获取 选择器 字符串 c#for windows 应用程序 phone | 更新日期: 2023-09-27 18:28:43
这是我的xaml代码
<toolkit:ListPicker SelectionChanged="CountryLP_SelectionChanged" Header="--Select Mode--" ItemsSource="{Binding CountryListdata, ElementName=this}" x:Name="CountryLP" VerticalAlignment="Center">
<toolkit:ListPicker.ItemTemplate>
<DataTemplate >
<StackPanel>
<TextBlock Text="{Binding country}" />
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.ItemTemplate>
<toolkit:ListPicker.FullModeItemTemplate>
<DataTemplate >
<StackPanel>
<TextBlock Text="{Binding country}" />
</StackPanel>
</DataTemplate>
</toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>
在页面加载过程中,控件自动转到事件CountryLP_SelectionChanged我想获得所选项目字符串
这是一个c#代码,用于将值设置为listpicker
//to get values from webservice
public class CountryList
{
public String id{ get; set; }
public String country{ get; set; }
}
private void CountryLP_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (CountryLP.SelectedItem == null)
return;
//var s= (sender as ListPicker).SelectedItem;
CountryList s= CountryLP.SelectedItem as CountryList;
//string s= CountryLP.SelectedItem as string;
//string s= CountryLP.SelectedItem.toString();
System.Diagnostics.Debug.WriteLine(s);
}
我尝试了几种方法,但没有得到字符串值。有人能帮我吗?
代码看起来不错。您可以通过将此异常添加到配置文件中来处理此异常。
<configuration>
<runtime>
<legacyCorruptedStateExceptionsPolicy enabled="true" />
</runtime>
</configuration>
也可以尝试
var content = ((ListPickerItem)CountryLP.SelectedItem).Content;