我如何访问我选择的项目?列表框

发布于 2024-11-02 00:51:10 字数 1529 浏览 1 评论 0原文

我的 xaml 页面上有一个名为 MainListBox 的 ListBox。我可以获得选择的索引,但是如何从所选项目中获取数据?

我的 MainListBox_SelectionChanged :

private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int noteID1 = MainListBox.SelectedIndex+1;

        if (MainListBox.SelectedIndex != null)
        {


            //I can get the index that get selected, 
            Debug.WriteLine(MainListBox.SelectedIndex);



        }

         MainListBox.SelectedIndex = -1;

    }

我的 XAML :

<ListBox x:Name="MainListBox" Margin="6,0,0,0" ItemsSource="{Binding Items}" SelectionChanged="MainListBox_SelectionChanged" Height="578" VerticalAlignment="Bottom" Grid.ColumnSpan="3">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432">
                        <TextBlock x:Name="ItemText" Text="{Binding noteName}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock x:Name="DetailsText" Text="{Binding noteText}" Margin="10,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        <TextBlock x:Name="noteIdText" Text="{Binding noteID}" Margin="10,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

请有人指导我,谢谢。 :)

i have ListBox on my xaml page called MainListBox. i can get index that get selected, but how can i get the data from selected item ?

My MainListBox_SelectionChanged :

private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int noteID1 = MainListBox.SelectedIndex+1;

        if (MainListBox.SelectedIndex != null)
        {


            //I can get the index that get selected, 
            Debug.WriteLine(MainListBox.SelectedIndex);



        }

         MainListBox.SelectedIndex = -1;

    }

my XAML :

<ListBox x:Name="MainListBox" Margin="6,0,0,0" ItemsSource="{Binding Items}" SelectionChanged="MainListBox_SelectionChanged" Height="578" VerticalAlignment="Bottom" Grid.ColumnSpan="3">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432">
                        <TextBlock x:Name="ItemText" Text="{Binding noteName}" Margin="-2,-13,0,0" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock x:Name="DetailsText" Text="{Binding noteText}" Margin="10,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        <TextBlock x:Name="noteIdText" Text="{Binding noteID}" Margin="10,-6,0,3" Style="{StaticResource PhoneTextSubtleStyle}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

Please someone guide me, thanks. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一抹微笑 2024-11-09 00:51:11

假设您将 ListBoxItemsSource 属性绑定到的 Items 对象是 MyDataObject 类的对象集合。然后,在选择更改回调中使用以下内容:

MyDataObject obj = ( (sender as FrameworkElement).DataContext ) as MyDataObject;
int noteID = obj.noteID;

Let's say the Items object that you're binding the ListBox's ItemsSource property to is a collection of objects of the class MyDataObject. Then, within the selection changed callback use the following:

MyDataObject obj = ( (sender as FrameworkElement).DataContext ) as MyDataObject;
int noteID = obj.noteID;
沐歌 2024-11-09 00:51:11

感谢大家的快速回复。

终于我明白了。

 if (MainListBox.SelectedItem != null)
        {


            var data = MainListBox.SelectedItem as Notes;

            NavigationService.Navigate(new Uri("/DetailsPage.xaml?noteID=" + data.noteID, UriKind.Relative));


        }

Thanks everyone for fast reply.

finally i figure it out.

 if (MainListBox.SelectedItem != null)
        {


            var data = MainListBox.SelectedItem as Notes;

            NavigationService.Navigate(new Uri("/DetailsPage.xaml?noteID=" + data.noteID, UriKind.Relative));


        }
橪书 2024-11-09 00:51:11

SelectedItem 是 Items 中的实体。可以直接转换为实体类型。

另外,SelectedItem 必须位于 WP7 中的 System.Windows.Control.ListBox 中。这是文档:
http://msdn.microsoft.com/en-us /library/system.windows.controls.listbox.aspx
http://msdn.microsoft.com /en-us/library/system.windows.controls.primitives.selector.selecteditem.aspx

SelectedItem is the entity in the Items. You can directly convert to the entity type.

Also SelectedItem must be in System.Windows.Control.ListBox in WP7. Here is the documentation:
http://msdn.microsoft.com/en-us/library/system.windows.controls.listbox.aspx
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selecteditem.aspx

静谧 2024-11-09 00:51:11
private void MyList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataModel data = (sender as ListBox).SelectedItem as DataModel;
    // data.MyPropertyHere;
}
private void MyList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataModel data = (sender as ListBox).SelectedItem as DataModel;
    // data.MyPropertyHere;
}
清醇 2024-11-09 00:51:10

您尝试过 MainListBox.SelectedItem 吗?

var data = MainListBox.Selecteditem as [绑定到列表框的类类型] ;

have you tried MainListBox.SelectedItem ?

var data = MainListBox.Selecteditem as [type of class bounded to listbox] ;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文