当我点击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;
}
}
Event
上课:
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; }
}
答案 0 :(得分:1)
在txtEventName
TextBlock中,您必须添加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"/>