Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WP7列表框SelectedItem_C#_Silverlight_Windows Phone 7 - Fatal编程技术网

C# 使用WP7列表框SelectedItem

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

当导航到Listbox SelectionChanged上的另一个页面时,我希望传递textblock值,而不是发送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
}

创建一个新的数据绑定应用程序

查看生成的源代码。它显示了一个如何准确执行此操作的示例。

请尝试以下操作:
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);
                }
            }
        }