![](/img/trans.png)
[英]How to reset ListBox SelectedItem to it's orginal state. WP7
[英]Working with WP7 ListBox SelectedItem
导航到Listbox SelectionChanged上的另一个页面时,我想随其传递文本块值,而不是发送listbox的项目索引。 我怎样才能做到这一点???
为了获得选定的ListBoxItem,您需要做的是:
private void btnGetSelected_Click(object sender, RoutedEventArgs e)
{
ListBoxItem selectedItem =this.listBox.ItemContainerGenerator.ContainerFromItem(this.listBox.SelectedItem) as ListBoxItem;
var textblock = selectedItem.Content
}
创建一个新的DataBound应用程序。
查看生成的源。 它显示了如何精确执行此操作的示例。
请尝试以下操作:
XAML源代码中的ListBox:
<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>
在.xaml.cs代码中:
public void SurahsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string r = ((ListBox)sender).SelectedValue.ToString();
NavigationService.Navigate(new Uri("/page.xaml?selecteItem=" + r, UriKind.Relative));
}
在page.xaml.cs代码中:
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.