C# 使用WP7列表框SelectedItem
当导航到Listbox SelectionChanged上的另一个页面时,我希望传递textblock值,而不是发送Listbox的项目索引。如何操作???要获取所选的ListBoxItem,您只需执行以下操作:C# 使用WP7列表框SelectedItem,c#,silverlight,windows-phone-7,C#,Silverlight,Windows Phone 7,当导航到Listbox SelectionChanged上的另一个页面时,我希望传递textblock值,而不是发送Listbox的项目索引。如何操作???要获取所选的ListBoxItem,您只需执行以下操作: private void btnGetSelected_Click(object sender, RoutedEventArgs e) { ListBoxItem selectedItem =this.listBox.ItemContainerGenerator.Contain
private void btnGetSelected_Click(object sender, RoutedEventArgs e)
{
ListBoxItem selectedItem =this.listBox.ItemContainerGenerator.ContainerFromItem(this.listBox.SelectedItem) as ListBoxItem;
var textblock = selectedItem.Content
}
创建一个新的数据绑定应用程序 查看生成的源代码。它显示了一个如何准确执行此操作的示例。请尝试以下操作:
XAML源代码中的列表框:
在page.xaml.cs代码中:
<ListBox x:Name="listBox" FontSize="26" SelectionChanged="listBox_SelectionChanged">
<ListBoxItem Content="Item1"/>
<ListBoxItem Content="Item2"/>
<ListBoxItem Content="Item3"/>
<ListBoxItem Content="Item4"/>
<ListBoxItem Content="Item5"/>
<ListBoxItem Content="Item6"/>
</ListBox>
public void SurahsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string r = ((ListBox)sender).SelectedValue.ToString();
NavigationService.Navigate(new Uri("/page.xaml?selecteItem=" + r, UriKind.Relative));
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
try
{
string selectedItem= "";
if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedItem))
{
if(null != selectedItem) {
// your code
}
}
}
catch (Exception ex)
{
if (System.Diagnostics.Debugger.IsAttached)
{
MessageBox.Show(ex.Message);
}
}
}