gpt4 book ai didi

c# - ListBox 不显示来自数据库的信息

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:48 28 4
gpt4 key购买 nike

当我单击 ListBox 时,我注意到有两个项目。在我的数据库中有两个对象。当我运行我的程序时,ListBox 从数据库中获取两个对象,但不显示这两个对象的名称。

这是我的代码:

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

事件类:

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

最佳答案

txtEventName 文本 block 中,您必须添加 Text={Binding EventName}

EventName 或您想在 txtEventName 中显示的任何属性。

如果 eList 是一个字符串列表,那么您的 txtEventName 必须是这样的:

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

关于c# - ListBox 不显示来自数据库的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31499193/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com