Windows Phone 7的:列表框SelectionChanged事件 [英] Windows Phone 7: ListBox SelectionChanged event

查看:153
本文介绍了Windows Phone 7的:列表框SelectionChanged事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的XAML和Windows Phone 7 SDK。我开发一个Windows Phone 7的应用程序,我不知道如何从ListBox中检测到选定的项目。我使用的全景模板,这里是我的代码:

I'm new to XAML and Windows Phone 7 SDK. I'm developing an Windows Phone 7 application and I don't know how to detect selected item from the ListBox. I'm using panorama template, here is my code:

<controls:PanoramaItem Header="Basic">
   <ListBox Margin="0,0,-12,0" Name="MyListBox" SelectionChanged="Elementary_SelectionChanged">
      <ListBox.ItemTemplate>
         <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
               <Image Height="100" Width="100" Source="{Binding LevelPassedImage}" Margin="12,0,9,0"/>
               <StackPanel Width="311">
                  <TextBlock Name="lvlName" x:Uid="Elementary{Binding LevelId}" Text="{Binding LevelName}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
                  <TextBlock Text="{Binding LevelPassed}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}" />
               </StackPanel>
            </StackPanel>
         </DataTemplate>
      </ListBox.ItemTemplate>
   </ListBox>
</controls:PanoramaItem>

和C#代码:

MessageBox.Show(Elementary.SelectedItem.ToString()); //returns "LocalXmlParsing.XMLParser"



我使用XMLParser的,应用程序初始化代码:

I'm using XMLParser, app initialization code:

var parser = LocalXmlParsing.XMLParser.Instance;

StreamResourceInfo strm = Application.GetResourceStream(new Uri("Levels/ElementaryLevels.xml", UriKind.Relative));
StreamReader reader = new StreamReader(strm.Stream);
string data = reader.ReadToEnd();
parser.DataToParse = data;
parser.ParseStateData();
MyListBox.ItemsSource = parser.LevelCollection;

当我试图检测的SelectedItem ,列表框返回我这个字符串:LocalXmlParsing.XMLParser

When I'm trying to detect SelectedItem, ListBox returns me this string: "LocalXmlParsing.XMLParser".

推荐答案

看起来你正在使用的诺基亚的示例项目称为LocalXmlParsing。您仍然可以使用你的代码,但如果要检测的SelectedItem 你应该使用这样的:

It looks like you are using Nokia's Sample project called "LocalXmlParsing". You can still use your code, but if you want to detect SelectedItem you should use something like this:

LocalXmlParsing.Level selecteditem = (LocalXmlParsing.Level)myListBox.SelectedItem; //it will returns your element
MessageBox.Show(selecteditem.Id); //It will return the Id of SelectedItem (String). You should use yours: SelectedItem.MyElement

这篇关于Windows Phone 7的:列表框SelectionChanged事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆