user3411961
user3411961

Reputation: 125

ListBox does not display information from database

When I click on the ListBox I notice that there are two items. In my database there are two objects. When I run my program the ListBox gets the two objects from the database but does not display the name of the two objects.

Here are my codes:

XAML:

<TextBox x:Name="txtSearch" Background="White" GotFocus="txtSearch_Focus" TextChanged="txtSearch_TextChanged" Text="search" FontSize="30"  Height="57" Margin="19,10,19,0" Grid.Row="1" />

<!--TODO: Content should be placed within the following grid-->
<Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,72,19,0">

    <!--<ScrollViewer>-->
        <ListBox Background="Black" x:Name="listBox" FontSize="26" Margin="0,10,0,0">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock x:Name="txtEventName" TextWrapping="Wrap" Foreground="White" Width="300" Margin="10,15,0,0" Height="55"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    <!--</ScrollViewer>-->
</Grid>

XAML.cs:

protected async override void OnNavigatedTo(NavigationEventArgs e)
{
    var eventList = await App.MobileService.GetTable<Event>().ToListAsync();

        foreach(Event ename in eventList)
        {
            eList.Add(ename.EventName);
        }

      this.listBox.ItemsSource = eList;

        this.navigationHelper.OnNavigatedTo(e);
    }

    private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (eList != null)
        {
            var items = new List<string>();
            foreach (var item in eList)
            {
                if (item.Contains(((TextBox)sender).Text))
                    items.Add(item);
            }
            //this.listBox.ItemsSource = items;
        }
    }

    bool hasBeenClicked = false;

    private void txtSearch_Focus(object sender, RoutedEventArgs e)
    {
        if (!hasBeenClicked)
        {
            txtSearch.Text = String.Empty;
            hasBeenClicked = true;
        }
}

Event class:

public class Event : IBusinessEntityBase
{
    public string Id { get; set; }
    public string Image { get; set; }
    public string EventName { get; set; }

    public string Desc { get; set; } //Description of Event

    public string Category { get; set; }

    public string Location { get; set; }

    public DateTime Date { get; set; }    //Should be data type Date

    public DateTime StartingTime { get; set; }    //Should be of different type (Time?)

    //public DateTime EndingTime { get; set; }      //Should be of different type (Time?)

    //public Boolean PinnedEvent { get; set; }
    //public string PinnedEvent { get; set; }
}

Upvotes: 2

Views: 68

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39966

in the txtEventName TextBlock you must add Text={Binding EventName}.

EventName or whatever property you want to show in txtEventName.

if eList is a list of strings then your txtEventName must be like this:

 <TextBlock x:Name="txtEventName" Text="{Binding}" 
     TextWrapping="Wrap" Foreground="White" Width="300" Margin="10,15,0,0" Height="55"/>

Upvotes: 1

Related Questions