LongListSelector C#-如何从分组数据中获取SelectedItem作为字符串Windows Phone
本文关键字:SelectedItem 获取 字符串 Phone Windows 数据 C#- LongListSelector | 更新日期: 2023-09-27 18:22:23
我正在用C#构建我的第一个WP Silverlight 8.1应用程序,所以还是个新手!
我的应用程序中有一个LongListSelector,允许用户选择房间。当用户点击房间时,我希望房间名称(例如"N01"、"N07"、"N12"——请参阅下面的代码!)存储为字符串。
我将房间存储在一个分组列表中——列表"dataSource"绑定到LongListSelector(btw称为"longListSelectorState")。
这是我的C#代码列表:
namespace WPLongListSelectorDemo
{
public partial class MainPage : PhoneApplicationPage
{
List<RoomList> dataSource;
// Constructor
public MainPage()
{
InitializeList();
InitializeComponent();
List<RoomGroup<RoomList>> DataSource = RoomGroup<RoomList>.CreateGroups(dataSource,
System.Threading.Thread.CurrentThread.CurrentUICulture,
(RoomList s) => { return s.RoomName; }, true);
longListSelectorState.ItemsSource = DataSource;
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
private void InitializeList()
{
dataSource = new List<RoomList>();
//North Rooms - Classrooms
dataSource.Add(new RoomList("N01", "North"));
dataSource.Add(new RoomList("N02", "North"));
dataSource.Add(new RoomList("N03", "North"));
dataSource.Add(new RoomList("N04", "North"));
dataSource.Add(new RoomList("N05", "North"));
dataSource.Add(new RoomList("N06", "North"));
dataSource.Add(new RoomList("N07", "North"));
dataSource.Add(new RoomList("N08", "North"));
dataSource.Add(new RoomList("N09", "North"));
dataSource.Add(new RoomList("N10", "North"));
dataSource.Add(new RoomList("N11", "North"));
dataSource.Add(new RoomList("N12", "North"));
dataSource.Add(new RoomList("N13", "North"));
dataSource.Add(new RoomList("N14", "North"));
dataSource.Add(new RoomList("N15", "North"));
dataSource.Add(new RoomList("N16", "North"));
dataSource.Add(new RoomList("N17", "North"));
dataSource.Add(new RoomList("N18", "North"));
dataSource.Add(new RoomList("N19", "North"));
dataSource.Add(new RoomList("N20", "North"));
dataSource.Add(new RoomList("N21", "North"));
这个清单实际上比这个长,但我在这里把它减少了一点。列表中有100多个元素。
我尝试使用以下代码从列表中获取SelectedItem,然后将其显示在消息框中。
private void longListSelectorState_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedRoom = longListSelectorState.SelectedItem;
MessageBox.Show(selectedRoom.ToString());
}
但消息框只显示"WPLongListSelectorDemo.RoomList"作为SelectedItem。WPLongListSelector是名称空间,RoonList是列表名称,所以我想这是有道理的,但这不是我想要的!例如,我希望它说"N01"。
如何从列表中获取实际房间作为字符串形式的SelectedItem?
谢谢!
我不知道分组列表到底是什么,但如果你的RoomList类看起来像这样:
internal class RoomList
{
public string Location { get; set; }
public string ClassNumber { get; set; }
}
然后创建RoomList的新实例
RoomList selectedRoom = new RoomList()
{
Location = "north",
ClassNumber = "n01"
}
如果调用selectedRoom.ToString(),则会得到"WPLongListSelectorDemo.RoomList",因为您将类型本身转换为字符串(返回类型的命名空间和名称),而不是实例selectedRoom的字符串属性。
相反,你应该这样做:
MessageBox.Show(selectedRoom .Location.ToString())
或
MessageBox.Show(selectedRoom.ClassNumber.ToString()).
(在这种情况下,您甚至不需要ToString(),因为Location和ClassNumber都是字符串。)
您也可以覆盖RoomList:的ToString()方法
internal class RoomList
{
public string Location { get; set; }
public string ClassNumber { get; set; }
public override string ToString()
{
return Location + ", " + ClassNumber;
}
}
在这种情况下,selectedRoom.ToString()将返回'north,n01'。