我所做的是搜索事件的搜索功能。
我的问题是,当我进入页面并将ObservableCollection
计数设置为0时,会触发一个属性,但我的数据库中有事件详细信息。
这应该如何工作是当我进入页面时,有一个relay命令,它执行并从数据库中检索所有数据并将其放入可观察集合中,并将事件名称显示为ListBox
。我只有当我输入一个角色然后才能检测到这些事件。
以下是我的代码:
XAML:
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Loaded">
<Core:InvokeCommandAction Command="{Binding Searchfromhomepage.EventSearch, Mode=OneWay}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
<Grid x:Name="LayoutRoot">
<Grid.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Grid.ChildrenTransitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--<TextBox x:Name="txtSearch" Background="White" Text="{Binding Path=HomePage.TxtEntered, UpdateSourceTrigger=PropertyChanged}" FontSize="30" Height="57" Margin="19,10,19,0" Grid.Row="1" />-->
<TextBox x:Name="txtTest2" Text="{Binding Searchfromhomepage.Filter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,9.667,19,0">
<ListBox Background="Black" x:Name="listBox" FontSize="26" Margin="0,10,0,0" ItemsSource="{Binding Searchfromhomepage.FilteredNames, Mode=OneWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="txtEventName" TextWrapping="Wrap" Text="{Binding EventName}" Tapped="txtEventName_Tapped" IsTapEnabled="True" Foreground="White" Width="300" Margin="10,15,0,0" Height="55"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
viewmodel codes:
private static ObservableCollection<Event> _searchEventCollection = new ObservableCollection<Event>();
public static ObservableCollection<Event> SearchEventCollection
{
get { return _searchEventCollection; }
set { _searchEventCollection = value; }
}
//search from homepage event section
private RelayCommand _eventSearch;
/// <summary>
/// Gets the EventSearch.
/// </summary>
public RelayCommand EventSearch
{
get
{ return _eventSearch
?? (_eventSearch = new RelayCommand(
async () =>
{
SearchEventCollection.Clear();
var eventList = await App.MobileService.GetTable<Event>().ToListAsync();
foreach (Event ename in eventList)
{
SearchEventCollection.Add(new Event
{
Id = ename.Id,
EventName = ename.EventName,
Date = ename.Date,
Location = ename.Location,
Desc = ename.Desc
});
}
}));
}
}
private string filter;
public String Filter
{
get
{
return this.filter;
}
set
{
this.filter = value;
RaisePropertyChanged("FilteredNames");
}
}
public List<Event> FilteredNames
{
get
{
if (filter == "")
{
return SearchEventCollection.ToList();
}
else
{
return (from name in SearchEventCollection where name.EventName.ToUpper().StartsWith(filter.ToUpper()) select name).ToList();
}
}
}
public searchfromhomepageViewModel()
{
filter = "";
}
答案 0 :(得分:0)
在您的视图模型代码中设置_eventSearch命令,如此
public RelayCommand _eventSearch = new RelayCommand(EventSearch);