簡體   English   中英

從選定的數據綁定列表框項中獲取值

[英]Get value from selected databound listbox item

所以我真的不知道如何提出這個問題,但這正是我想要實現的目標。 我有一個包含此代碼的列表框:

<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>

並將Azure App Service用於我的數據庫。 我使用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;

}

我想要做的是當用戶選擇他需要重定向到新頁面的列表框項目時。 但是在這個新頁面上我需要事件的id(數據庫中的行)。 所以我可以使用此id來檢索事件的所有進一步信息。

也許我需要使用{Binding id}將id綁定到listboxitem但是我將它綁定到什么? 我可以隱藏的文本塊嗎? 如果我將它綁定到某些東西,我該如何檢索它?

這是我用來將用戶重定向到新頁面的方法:

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

如果有不同的方式來獲取有關所選事件的信息,請告訴我們。

我將衷心感謝您的幫助。

lb.SelectedItem獲取SelectedItem並將其轉換為其對應的類。 從那里,你可以獲得所選項目的IP。 現在將此值作為第二個參數傳遞給Frame.Navigate

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

要在下一頁上獲得此值

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