简体   繁体   English

从选定的数据绑定列表框项中获取值

[英]Get value from selected databound listbox item

So I really don't know how to ask this question but this is what I am trying to achieve. 所以我真的不知道如何提出这个问题,但这正是我想要实现的目标。 I got a list box with this code: 我有一个包含此代码的列表框:

<ListBox 
    x:Name="lb"
    Margin="0,0,0,100"
    Background="#212B34"
    BorderThickness="0"
    Padding="0"
    SelectionChanged="lb_SelectionChanged">

    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
            <Setter Property="Padding" Value="0,8,0,8"></Setter>
        </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid Height="152" BorderThickness="10,0,0,0" BorderBrush="#2E66AA" Background="#161C22" Padding="8,8,8,8">
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"></RowDefinition>
                    <RowDefinition Height="25"></RowDefinition>
                    <RowDefinition Height="25"></RowDefinition>
                    <RowDefinition Height="60"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="4*"></ColumnDefinition>
                    <ColumnDefinition Width="2*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <TextBlock Foreground="#2E66AA" Text="{Binding Location}" FontSize="18" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"></TextBlock>
                <TextBlock Foreground="White" Text="{Binding Description}" FontSize="12" Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" TextTrimming="WordEllipsis" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" TextWrapping="Wrap"></TextBlock>
                <TextBlock Foreground="White" Text="{Binding Date}" FontSize="18" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" TextAlignment="Right"></TextBlock>
                <TextBlock Foreground="White" Text="{Binding Time}" FontSize="18" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Stretch" TextAlignment="Right"></TextBlock>
                <TextBlock Foreground="#2E66AA" FontSize="24" Grid.Row="3" Grid.Column="1" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" TextAlignment="Right">
                    <Run Text="$ "/>
                    <Run Text="{Binding Price}"/>
                    <Run Text=";"/>
                </TextBlock>
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

And use Azure App Service for my database. 并将Azure App Service用于我的数据库。 I bind the data to the listbox using itemsource: 我使用itemsource将数据绑定到列表框:

private async void loadEvents_Click(object sender, RoutedEventArgs e)
{
    IMobileServiceTable<eventTable> eventList = App.MobileService.GetTable<eventTable>();


    MobileServiceCollection<eventTable, eventTable> events = await eventList
        .Where(ProductTable => ProductTable.id != "")
        .ToCollectionAsync();

    lb.ItemsSource = events;

}

What I want to do is when a user selects a listbox item he needs to be redirected to a new page. 我想要做的是当用户选择他需要重定向到新页面的列表框项目时。 But on this new page I need the id of the event (row in the database). 但是在这个新页面上我需要事件的id(数据库中的行)。 So I can use this id to retrieve all further information of the event. 所以我可以使用此id来检索事件的所有进一步信息。

Maybe I need to bind the id to the listboxitem by using {Binding id} but what do I bind it to? 也许我需要使用{Binding id}将id绑定到listboxitem但是我将它绑定到什么? A textblock wich I can make invisible? 我可以隐藏的文本块吗? And if I bind it to something how do I retrieve it? 如果我将它绑定到某些东西,我该如何检索它?

This is what I use to redirect the user to a new page: 这是我用来将用户重定向到新页面的方法:

private void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    this.Frame.Navigate(typeof(eventPage));
}

If there is a different way to get al information about the selected event please tell. 如果有不同的方式来获取有关所选事件的信息,请告诉我们。

I would really appreciate your help. 我将衷心感谢您的帮助。

Get the SelectedItem from lb.SelectedItem and typecast to its corresponding class. lb.SelectedItem获取SelectedItem并将其转换为其对应的类。 From that, you can get the selected item's Ip. 从那里,你可以获得所选项目的IP。 Now pass this value as the second parameter to Frame.Navigate . 现在将此值作为第二个参数传递给Frame.Navigate

private void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    this.Frame.Navigate(typeof(eventPage), (eventTable)lb.SelectedItem);
}

To get this value on the next page 要在下一页上获得此值

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    var selectedItem = (eventTable)e.Parameter;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM