簡體   English   中英

使用WP7 ListBox SelectedItem

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM