0

XAML と Windows Phone 7 SDK は初めてです。Windows Phone 7 アプリケーションを開発していますが、ListBox から選択された項目を検出する方法がわかりません。私はパノラマテンプレートを使用しています。これが私のコードです:

<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、アプリの初期化コードを使用しています:

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と、ListBox から「LocalXmlParsing.XMLParser」という文字列が返されます。

4

2 に答える 2

1

「LocalXmlParsing」というNokia のサンプル プロジェクトを使用しているようです。コードを引き続き使用できますが、検出したい場合SelectedItemは、次のようなものを使用する必要があります。

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
于 2013-08-10T07:33:32.207 に答える
1

選択した項目は ListBoxItem ではありませんが、実際には ItemSource を介して ListBox にバインドしたオブジェクトの型です。したがって、それを ListBoxItem にキャストすると、null オブジェクトが返されます。

ListBox.ItemsSource = new List<myObject>() { new myObject(), new myObject() };
ListBox.SelectedIndex = 1;
var selectedObject = ListBox.SelectedItem as myObject;
于 2013-08-09T17:50:41.607 に答える